简体   繁体   English

如何通过 JavaScript 设置输入隐藏字段的值?

[英]How to set the value of a input hidden field through JavaScript?

I am trying to call the resetyear function and it's getting called also, but the flow stops at alert("over") .我正在尝试调用resetyear函数,它也被调用了,但流程在alert("over")处停止。 It doesn't transfer its control to resetmaster .它不会将其控制权转移给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);
}

For me it works:对我来说它有效:

document.getElementById("checkyear").value = "1";
alert(document.getElementById("checkyear").value);

http://jsfiddle.net/zKNqg/ http://jsfiddle.net/zKNqg/

Maybe your JS is not executed and you need to add a function() {} around it all.也许你的 JS 没有执行,你需要在它周围添加一个 function() {}。

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="">移到开头。

It seems to work fine in Google Chrome.它似乎在 Google Chrome 中运行良好。 Which browser are you using?您使用的是哪个浏览器? Here the proof http://jsfiddle.net/CN8XL/这里的证明http://jsfiddle.net/CN8XL/

Anyhow you can also access to the input value parameter through the document.FormName.checkyear.value .无论如何,您还可以通过document.FormName.checkyear.value访问输入值参数。 You have to wrap in the input in a <form> tag like with the proper name attribute, like shown below:您必须将输入包裹在<form>标签中,就像使用适当的name属性一样,如下所示:

<form name="FormName">
    <input type="hidden" name="checkyear" id="checkyear" value="">
</form>

Have you considered using the jQuery Library?您是否考虑过使用jQuery库? Here are the docs for .val() function .这是.val()函数文档

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.我在您发布的内容中看到了一些服务器“if”代码,因此可能不满足呈现 javascript 的条件。 So, on the page you working on, right-click -> view source.因此,在您处理的页面上,右键单击 -> 查看源代码。 Try to find the js code there.尝试在那里找到 js 代码。 Please tell us if you found the code on the page.请告诉我们您是否在页面上找到了代码。

Your code for setting value for hidden input is correct.您为隐藏输入设置值的代码是正确的。 Here is the example .这是示例 Maybe you have some conditions in your if statements that are not allowing your scripts to execute.也许您的if语句中有一些条件不允许您的脚本执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM