如何通过非标准属性访问输入元素?

How to access an input element by a non-standard attribute?

我有几个输入字段,其中 name 和 id 属性根据在哪个服务器上推出而变化,并且 class 也不是唯一的。每个输入字段的唯一唯一属性在 "roiname".
中 我如何使用 jQuery 以跨服务器工作的方式获取此字段的值?

<input roiname="smallLocations" name="__field_13102" id="e5634b55-d6cc-4b32-a5aa-2120b4845dbb" type="number" class="form-control FormTextbox__Input" placeholder="4" value="" min="0" step="1" data-f-datainput="">

我试过这样的事情:

var smallLocations = $(":input").attr("smallLocations").val();

但这没有用。

您不是在寻找 属性 的值,而是在寻找 输入 的值。 (此外,您的 input 没有 具有 名为 "smallLocations" 的属性,这是其中一个属性的值。)

像这样:

$('input[roiname="smallLocations"]').val()

您当前的逻辑不起作用,因为您正在使用 attr(),它 检索属性的值 ,它没有找到该属性的元素。

要解决此问题,您需要使用属性选择器:

var roiname = $(':input[roiname]').val();
console.log(roiname);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input roiname="smallLocations" value="123" name="__field_13102" id="e5634b55-d6cc-4b32-a5aa-2120b4845dbb" type="number" class="form-control FormTextbox__Input" placeholder="4" value="" min="0" step="1" data-f-datainput="">

使用 sizzle 属性选择器

var smallLocations = $("input[roiname]").val();

文档在这里

https://api.jquery.com/has-attribute-selector/