Javascript 使用 jquery 设置隐藏输入的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4802999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set value of hidden input with jquery
提问by TonyM
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?
但它不起作用。我的代码有什么问题?
回答by
You should use val
instead of value
.
你应该使用val
而不是value
.
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('input[name="testing"]').val('Work!');
});
</script>
回答by gzveri
This worked for me:
这对我有用:
$('input[name="sort_order"]').attr('value','XXX');
回答by Haim Evgi
$('input[name="testing"]').val(theValue);
回答by Dat Nguyen
var test = $('input[name="testing"]:hidden');
test.val('work!');
回答by arteagasoft
Suppose you have a hidden input, named XXX, if you want to assign a value to the following
假设您有一个隐藏输入,名为 XXX,如果您想为以下内容赋值
<script type="text/javascript">
$(document).ready(function(){
$('#XXX').val('any value');
})
</script>
回答by Maykonn
回答by nzrytmn
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");