简体   繁体   English

添加输入值并在加载时提交表单

[英]adding an input value and submitting the form on load

I have the following form: 我有以下表格:

<form action="http://example.co.uk/order" method="post" id="voucher" class="AVAST_PAM_nonloginform">
  <fieldset>
    <h4>Vouchers</h4>
    <input type="text" class="discount_name form-control" id="discount_name" name="discount_name" value="">
    <input type="hidden" name="submitDiscount">
    <button type="submit" name="submitAddDiscount" class="button btn btn-default button-small"><span>OK</span></button>
  </fieldset>
</form>

and am using the following script: 并正在使用以下脚本:

<script>
window.onload = function(){
document.getElementById(\"discount_name\").value = \"50681\";
}
</script>

to populate the input. 填充输入。 I then use: 然后,我使用:

<script>
window.onload = function(){
document.forms['voucher'].submit();
}
</script>

to activate the submit. 激活提交。

However, use the second script, it stops the "50681" from being inputted into the text box (instead submits a blank input). 但是,使用第二个脚本,它将阻止将“ 50681”输入到文本框中(而是提交空白输入)。

Originally I had the code as : 最初,我的代码为:

  <script>
    window.onload = function(){
    document.getElementById(\"discount_name\").value = \"50681\";
document.forms['voucher'].submit();
    }
    </script>

(I split it up thinking it may be a timing issue). (我认为可能是时间问题而将其拆分)。

Any ideas? 有任何想法吗?

ps the reason for the backslash's is due to it currently being run under php until I can get it working ps反斜杠的原因是由于它目前在php下运行,直到我可以正常工作为止

The issue seems to be with (\\"discount_name\\").value = \\"50681\\"; 问题似乎出在(\\"discount_name\\").value = \\"50681\\"; & document.forms['voucher'].submit(); document.forms['voucher'].submit();

In either of the case you can avoid \\ & for form you need to target by the index number. 在任何一种情况下,都可以避免使用\\ &,因为您需要使用index号作为目标。 Assuming there is only one form present , so passing 0 in index 假设只存在一种形式,则在索引中传递0

 window.onload = function(){
    document.getElementById("discount_name").value = "50681";
    document.forms[0].submit();
    }

Note: In the demo I have changed the action url to https else it will prohibit to make call from jsfiddle. 注意:在演示中,我已将操作网址更改为https否则它将禁止从jsfiddle进行调用。 In your case you can still keep http in code 您仍然可以在代码中保留http

DEMO USING ID 示范使用ID

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

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