在JavaScript中,您可以使用以下两种方法来获取表单中的隐藏字段值:
document.getElementById('隐藏字段ID')。value
document.formName.elements['隐藏字段名称'].value
在这里查看示例:
<html>
<body>
<scripttype="text/javascript">
functionprintIt(){
alert(document.getElementById('abcId').value);
alert(document.formName.elements['abcName'].value);
}
</script>
<h1>AccessHiddenvalueinJavaScript</h1>
<formname="formName">
<inputtype=hiddenid="abcId"name="abcName"value="测试文字"/>
<inputtype=buttonvalue="GetValue"onclick="printIt()"/>
</form>
</body>
</html>
如果单击该按钮,将提示隐藏字段值“测试文字”。