简体   繁体   English

我该怎么读出来? <input> 通过name属性的值

[英]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.

相关问题 我有一个输入为图像的表单,我无法弄清楚如何将输入标签的name属性发送到mysql数据库? - I have a form with input as images and I can not figure out how to send the name attribute of the input tag to mysql database? 当我只知道它的“名称” - 属性时,如何通过jQuery设置input-element的值 - How do I set the value of an input-element via jQuery, when I only know it's “name”-attribute 如何通过jquery更改输入名称的一部分? - How can I change a part of input's name via jquery? 如何通过ajax更新输入文本的值? - how can I update value of input text via ajax? 如何读取查询动态生成的输入值? - How can I read the input value dynamically generated by the Query? 如何使用jQuery从输入数字中读取值? - How can I read value from input number by using jQuery? 我如何在运行时检索输入类型的名称值 - HOw can i retrieve name value of input type at run time 使用数据属性和jQuery,如何通过数据属性名称和数据属性值选择元素 - Using data attributes and jQuery how can I select elements by data attribute name AND data attribute value 如何读取通过表单输入的电子邮件地址的域名 - How to read the domain name of an email address input via a form 除了 vuejs 中的 value 属性,我可以绑定到其他输入属性吗 - can I bind to other input attribute besides value attribute in vuejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM