[英]Set value of hidden input with jQuery
I'm trying to set the value of the hidden field below using jQuery.我正在尝试使用 jQuery 设置下面隐藏字段的值。
<input type="hidden" value="" name="testing" />
I'm using this code:我正在使用这段代码:
var test = $("input[name=testing]:hidden");
test.value = 'work!';
But its not working.但它不起作用。 What's wrong with my code?我的代码有什么问题?
You should use val
instead of value
.您应该使用val
而不是value
。
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('input[name="testing"]').val('Work!');
});
</script>
这对我有用:
$('input[name="sort_order"]').attr('value','XXX');
$('input[name="testing"]').val(theValue);
Suppose you have a hidden input with id XXX, if you want to assign a value to the following假设您有一个 id 为 XXX 的隐藏输入,如果您想为以下内容赋值
<script type="text/javascript">
$(document).ready(function(){
$('#XXX').val('any value');
})
</script>
var test = $('input[name="testing"]:hidden');
test.val('work!');
You don't need to set name , just giving an id is enough.您不需要设置 name ,只需提供一个 id 就足够了。
<input type="hidden" id="testId" />
and than with jquery you can use 'val()' method like below:与 jquery 相比,您可以使用'val()'方法,如下所示:
$('#testId').val("work");
you can set value to input hidden filed in this way also您也可以通过这种方式将值设置为输入隐藏字段
<input type="hidden" value="" name="testing" id-"testing" />
document.getElementById("testing").value = 'your value';
this is one of the method .这是方法之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.