一般来说,你所拥有的看起来不错。我在这里为您创建了一个示例,以显示在 jQuery 中读取和设置值:http: //jsfiddle.net/T2v8K/2。这应该可以帮助您进行调试。
HTML:
<div>
<form id="myForm">
<table>
<tr>
<td>normal input: <input id="newValue" name="newValue" value="1" /></td>
<td>hidden input: <input id="item1" name="item1" type="hidden" value="someDefaultValue"/></td>
</tr>
</table>
</form>
<button id="showVal" type="button">Show Hidden Input Value</button>
<button id="setVal" type="button">Set Hidden Input Value</button>
</div>
JavaScript / jQuery:
$( document ).ready(function() {
var beforeValue = $( "#item1" ).val();
//alert( "Before = " + beforeValue );
var afterValue = $( "#item1" ).val();
//alert( "After = " + afterValue );
$( "#showVal" ).click( function() {
ShowHiddenInputValue();
})
$( "#setVal" ).click( function() {
SetHiddenInputValue();
})
});
function ShowHiddenInputValue() {
//Show the current value of the hidden input
var hiddenInputValue = $( "#item1" ).val();
alert( "Hidden Input Value = " + hiddenInputValue );
}
function SetHiddenInputValue(){
//Get the value from the input
var newHiddenInputValue = $( "#newValue" ).val();
//Set the hidden input
$( "#item1" ).val(newHiddenInputValue);
ShowHiddenInputValue();
}
尝试像这样设置一个默认值,看看您是否正确读出了表单帖子中的值。
此外,如果您不确定 jQuery 是否正常工作(可能没有正确引用),您可以通过在 JavaScript 中检查类似这样的警报中的版本号来测试它:
alert($().jquery);