Javascript 添加输入值并在加载时提交表单
我有以下表格:Javascript 添加输入值并在加载时提交表单,javascript,forms,Javascript,Forms,我有以下表格: <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" na
<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>
凭证
好啊
我正在使用以下脚本:
<script>
window.onload = function(){
document.getElementById(\"discount_name\").value = \"50681\";
}
</script>
window.onload=函数(){
document.getElementById(\“折扣\名称\”)值=\“50681\”;
}
来填充输入。然后我使用:
<script>
window.onload = function(){
document.forms['voucher'].submit();
}
</script>
window.onload=函数(){
document.forms[‘凭证’].submit();
}
以激活提交
但是,使用第二个脚本,它会停止将“50681”输入到文本框中(而是提交空白输入)
最初我的代码是:
<script>
window.onload = function(){
document.getElementById(\"discount_name\").value = \"50681\";
document.forms['voucher'].submit();
}
</script>
window.onload=函数(){
document.getElementById(\“折扣\名称\”)值=\“50681\”;
document.forms[‘凭证’].submit();
}
(我认为这可能是一个时间问题,所以将其拆分)
有什么想法吗
p、 反斜杠的原因是因为它目前在php下运行,直到我能让它工作为止。问题似乎出在
(\“折扣\名称”)上。值=\“50681\”代码>&文档。表单['凭证]]。提交()代码>
在这两种情况下,您都可以通过索引
编号避免表单的\
。假设只存在一个表单,那么在索引中传递0
window.onload = function(){
document.getElementById("discount_name").value = "50681";
document.forms[0].submit();
}
注意:在演示中,我已将操作url更改为https
,否则将禁止从JSFIDLE调用。在您的情况下,您仍然可以在代码中保留http
之所以使用“\”是因为它目前在php中运行。它是prestashop,有多种形式。如何找到索引号?对于使用ID的多个表单,您可以使用ID更新您的示例以反映使用ID的情况吗?