本文介绍了如何在javascript中自动提交表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入文本字段的表单,每隔几秒钟输入一次数据并将其显示在表单字段中,让我们说:

 < input name =codeid =codetype =textsize =64maxlength =128/> 

和提交按钮,我的表单具有formname form1。
我希望在表单字段中的数据被更改后立即点击提交按钮。
我确实尝试了以下操作。在标题中,我添加了以下javascript:

 < SCRIPT LANGUAGE =javascript> 
函数send_data()
{
document.form1.submit();
}
< / SCRIPT>

以及表格:

 < input name =codeid =codetype =textsize =64maxlength =128onchange =send_data(); /> 

但它没有工作..





谢谢

>

 < form action =#> 
< input type =id =input/>
< button type =submit>< / button:>
< / form>

< script>
函数send_data(){
document.forms [0] .submit();


window.onload = function(){
var input = document.getElementById('input');
input.onchange = send_data;
}
< / script>

但我会补充一些注意事项:


  1. 我假设页面上只有一种形式。你会更安全地为表单分配ID并使用 getElementById 并引用该表单而不是 document.forms [x] 改变事件只会在你失去对输入的注意力之后才会发生,这可能是你想要的吗?只是确保它是预期的行为

  2. 不知道为什么你需要这样做,我会注意到它可能会非常恼人的用户,因为提交将触发新的页面加载。通过Ajax进行提交可能会更好,以免中断用户的浏览。如果你这样做,我强烈推荐一个JS库来处理这个问题。


I have a form with an input text field which imports data every couple of seconds and display it in the form field , let us say :

<input name="code" id="code" type="text" size="64" maxlength="128" />

and a submit button and my form has the formname form1.I want the submit button to be clicked as soon as the data in the form field is changed.I did try to do the following. In the header I did add the follwoing javascript:

<SCRIPT LANGUAGE="javascript">
function send_data()
{
document.form1.submit();
}
</SCRIPT>

and on the form :

<input name="code" id="code" type="text" size="64" maxlength="128" onchange="send_data();" />

but it didn`t work..

Any help ?

Thanks

解决方案

Something like this would work:

<form action="#">
    <input type="" id="input" />
    <button type="submit"></button:>
</form>

<script>
function send_data() {
    document.forms[0].submit();
}

window.onload = function(){
    var input = document.getElementById('input');
    input.onchange = send_data;
}
</script>

But I'd add a few caveats:

  1. I'm assuming there is only one form on the page. You would be safer assigning and ID to your form and using getElementById and referencing that instead of document.forms[x]
  2. The change event will only happen after you lose focus on the input, which I probably what you want? Just making sure it's expected behavior
  3. Without knowing why you need to do this, I'd note that it could potentially be very annoying to the user, as submission will trigger a new page load. You may be better off doing a submission via ajax so as not to disrupt the user's browsing. If you do this, I strongly recommend a JS library to handle this.

这篇关于如何在javascript中自动提交表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:35