使用 jQuery 设置隐藏输入的值

我正在尝试使用 jQuery 设置下面隐藏字段的值。

<input type="hidden" value="" name="testing" />

我用这个代码:

var test = $("input[name=testing]:hidden");
test.value = 'work!';

但是没有用,我的代码有什么问题吗?

254873 次浏览
$('input[name="testing"]').val(theValue);

You should use val instead of value.

<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('input[name="testing"]').val('Work!');
});
</script>

This worked for me:

$('input[name="sort_order"]').attr('value','XXX');
var test = $('input[name="testing"]:hidden');
test.val('work!');

Suppose you have a hidden input with id XXX, if you want to assign a value to the following

<script type="text/javascript">


$(document).ready(function(){
$('#XXX').val('any value');
})
</script>

To make it with jquery, make this way:

var test = $("input[name=testing]:hidden");
test.val('work!');

Or

var test = $("input[name=testing]:hidden").val('work!');

See working in this fiddle.

You don't need to set name , just giving an id is enough.

<input type="hidden" id="testId" />

and than with jquery you can use 'val()' method like below:

 $('#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 .