[英]How to get value from input type hidden with Javascript in correct way?
I have a code 我有一个密码
HTML : HTML:
<input type="hidden" name="test[]" id="test_0" value="123456789">
Javascript : Javascript:
$(".test").each(function(){
console.log('Test Value : '+ $(this).val());
});
Result : 结果:
Test Value = 测试值=
The question : 问题:
Why result of console.log for $(this).val() inside my Javascript code give result empty ? 为什么我的Javascript代码中$(this).val()的console.log结果使结果为空? And how to fix it without change the Javascript code? 以及如何在不更改Javascript代码的情况下进行修复?
Thank you 谢谢
The problem is that $(".test")
targets the class test
, not the name test. 问题在于$(".test")
针对类 test
而不是名称 test的。 In order to solve that by only changing the HTML, simply give the input the relevant class: 为了解决此问题,只需更改HTML,只需为输入提供相关的类:
<input type="hidden" name="test[]" class="test" id="test_0" value="123456789">
A working fiddle demonstrates this here . 一个工作的小提琴在这里证明了这一点 。
Hope this helps :) 希望这可以帮助 :)
test class is not defined. 测试类未定义。
try example, 试试例子
console.log('Test Value : '+ $('#test_0').val());
You are not targeting the desired element. 您没有针对所需的元素。
Try this: 尝试这个:
$("#test_0").each(function(){
console.log('Test Value : '+ $(this).val());
});
In the HTML, you have to add a class to input 在HTML中,您必须添加一个类以进行输入
The problem, is that you are not getting any element, because you haven't any element with that class. 问题在于,您没有获得任何元素,因为该类没有任何元素。 I think that's all. 我认为就这些。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.