[英]How can I read out an <input> value via the name attribute
I've got the following output in my HTML 我的HTML中有以下输出
<input type="hidden" value="28" name="variation_id">
This value is generated dynamically. 该值是动态生成的。 I want to write an If else statement that is dependant on this value ie the "28".
我想写一个依赖于这个值的If else语句,即“28”。 I'm not quite sure on how to target this value since it has no ID.
我不太清楚如何定位这个值,因为它没有ID。 Is there a code to read out this value, via the "name" attribute?
是否有代码通过“name”属性读出这个值?
I want to write the if else statement in Javascript. 我想在Javascript中编写if else语句。
Assuming you only have the one element with that name
: 假设您只有一个具有该
name
元素:
var elem = document.getElementsByName('variation_id')[0];
if (elem.value == '28') {
// it has that value, do something
}
else {
// it doesn't have that value, do something else.
}
Or, in more up to date browsers: 或者,在更新的浏览器中:
var elem = document.querySelector('input[name="variation_id"]');
if (elem.value == '28') {
// it has that value, do something
}
else {
// it doesn't have that value, do something else.
}
References: 参考文献:
就像这个一样
var val = document.getElementsByName('variation_id')[0].value;
你需要的是:
var value = document.getElementsByName("variation_id")[0].value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.