简体   繁体   English

根据属性中的字符串选择值

[英]Selecting value based on string in attribute

I'm working with the following bit of html and am trying to select it's value in either plain javascript or jquery. 我正在使用以下的html,我试图在普通的javascript或jquery中选择它的值。 The name attribute can vary so I can't use it as my selector, although it will always represent the same data (grade_level). name属性可能会有所不同,所以我不能将它用作我的选择器,尽管它总是代表相同的数据(grade_level)。 I think my best way of selecting it is via the string 'students.grade_level, but I'm not sure how to access it. 认为我选择它的最好方法是通过字符串'students.grade_level,但我不知道如何访问它。

<input type="hidden" name="UF-001008-1$formatnumeric=#########.#####" value="1" data-validation="{"maxlength":"10","isinteger":"true","type":"number","key":"students.grade_level"}">

I have so far not been able to select element's value. 到目前为止,我还没能选择元素的值。 I have tried: 我努力了:

var myvar = $( "input[data-validation~='students.grade_level']" ).val();
var myvar = $( "input:contains('students.grade_level')" ).val(); 

How else can I go about this? 我还能怎么做呢?

TYIA TYIA

The way that you have given your attribute is wrong, 你给出属性的方式是错误的,

<input type="hidden" name="UF-001008-1$formatnumeric=#########.#####" value="1" data-validation="{'maxlength':'10','isinteger':'true','type':'number','key':'students.grade_level'}">

Use quotes properly to cover your attributes, either escape the double quote or use single quotes instead. 正确使用引号来覆盖您的属性,要么escape double quote ,要么使用single quotes

And after correcting that you could use the *= attibute value contains selector to achieve what you want, 在纠正之后你可以使用*= attibute值包含选择器来实现你想要的,

var myvar = $( "input[data-validation*='students.grade_level']" ).val();

:contains() selector will not work out since it would search for matching text content. :contains()选择器将无法解决,因为它将搜索匹配的文本内容。 Also ~= attribute contains word selector wont work out as we do not have any word in our data-validation attribute. 另外~=属性包含单词选择器不会解决,因为我们的data-validation属性中没有任何单词。 Word in the sense, group of texts separated by a space. 在某种意义上的词,由空格分隔的一组文本。

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM