Html 如何通过javascript设置输入隐藏字段的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19232822/
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
how to set value of a input hidden field through javascript?
提问by Divyang
I am trying to call the resetyear function and its getting called also but the flow stops at alert("over"), it doesnt tranfer its control to resetmaster. Please suggest me something.
我正在尝试调用 resetyear 函数并且它也被调用,但是流程在警报(“结束”)处停止,它不会将其控制权转移给 resetmaster。请给我建议。
String flag = "";
flag = (String) session.getAttribute("flag");
System.out.println("flag = " + flag);
if (flag == null) {
flag = "";
}
if (flag.equals("yes")) {
%>
<script>
alert(1);
// resetyear();
dontreset();
//document.getElementById("checkyear").value = "1";
//alert(document.getElementById("checkyear").value);
</script>
<%} else if(flag.equals("no"))
{%>
<script>
alert(2);
//document.getElementById("checkyear").value = "2";
//alert(document.getElementById("checkyear").value);
resetyear();
</script>
<%}else{}%>
function resetyear(){
if(confirm("DO YOU WANT TO RESET THE YEAR?"))
{
alert("hello");
//document.forms['indexform'].action = "resetmaster";
//alert(document.forms['indexform'].action);
//document.forms['indexform'].submit();
alert("over");
form.action = "resetmaster";
form.submit();
alert(1);
}
回答by almo
For me it works:
对我来说它有效:
document.getElementById("checkyear").value = "1";
alert(document.getElementById("checkyear").value);
Maybe your JS is not executed and you need to add a function() {} around it all.
也许你的 JS 没有执行,你需要在它周围添加一个 function() {}。
回答by Matt H
You need to run your script after the element exists. Move the <input type="hidden" name="checkyear" id="checkyear" value="">
to the beginning.
您需要在元素存在后运行脚本。移动<input type="hidden" name="checkyear" id="checkyear" value="">
到开始。
回答by andreacanton
It seems to work fine in Google Chrome. Which browser are you using? Here the proof http://jsfiddle.net/CN8XL/
它似乎在 Google Chrome 中运行良好。您使用的是哪个浏览器?这里的证明http://jsfiddle.net/CN8XL/
Anyhow you can also access to the input value parameter through the document.FormName.checkyear.value
. You have to wrap in the input in a <form>
tag like with the proper name
attribute, like shown below:
无论如何,您还可以通过document.FormName.checkyear.value
. 您必须<form>
使用适当的name
属性将输入包裹在标签中,如下所示:
<form name="FormName">
<input type="hidden" name="checkyear" id="checkyear" value="">
</form>
Have you considered using the jQueryLibrary? Here are the docs for .val()
function.
回答by Alex Buyny
The first thing I will try - determine if your code with alerts is actually rendered. I see some server "if" code in what you posted, so may be condition to render javascript is not satisfied. So, on the page you working on, right-click -> view source. Try to find the js code there. Please tell us if you found the code on the page.
我将尝试的第一件事 - 确定您的带有警报的代码是否实际呈现。我在您发布的内容中看到一些服务器“if”代码,因此可能不满足呈现 javascript 的条件。因此,在您处理的页面上,右键单击 -> 查看源代码。尝试在那里找到 js 代码。请告诉我们您是否在页面上找到了代码。