Javascript onchange="this.form.submit()" 对比两个提交按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8315887/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:32:02  来源:igfitidea点击:

onchange="this.form.submit()" versus two submit buttons

javascriptforms

提问by Exit

With a pulldown triggering an onchange event to submit, it fails to work when two input submit buttons are present. It makes sense, so how do you specify exclusively which submit button to process?

下拉触发 onchange 事件提交,当存在两个输入提交按钮时它无法工作。这是有道理的,那么您如何专门指定要处理的提交按钮?

<form>
<select onchange="this.form.submit()"></select>
<input type="submit" name="submit1" id="submit1" value="Submit 1" />
<input type="submit" name="submit2" id="submit2" value="Submit 2" />
</form>

SolutionUsing the suggestion from below, this code will work so that the second submit button will execute if the onchange is trigged from the pulldown:

解决方案使用下面的建议,此代码将起作用,以便如果从下拉菜单触发 onchange 则第二个提交按钮将执行:

<form>
<select onchange="var e=document.getElementById('killbox'); var s=document.getElementById('submit1'); e.removeChild(s); this.form.submit();"></select>
<div id="killbox"><input type="submit" name="submit1" id="submit1" value="Submit 1" /></div>
<input type="submit" name="submit2" id="submit2" value="Submit 2" />
</form>

This is in a basic form, certainly doesn't hurt to throw the Javascript in to a function instead of using inline Javascript for the onchange.

这是一种基本形式,将 Javascript 放入函数中而不是使用内联 Javascript 进行 onchange 当然不会有什么坏处。

采纳答案by Tom van der Woerdt

I'm pretty sure you can simply call .submit()on the button itself: document.getElementById('submit1').submit()

我很确定你可以简单地调用.submit()按钮本身:document.getElementById('submit1').submit()

-edit-

-编辑-

Solution two: just remove one of the buttons (use removeChild(), which is a common DOM method).

解决方案二:删除其中一个按钮即可(使用removeChild(),这是一种常见的DOM方法)。

回答by Mathletics

You could fire a click event on the button you want to use to submit. It feels weird, but it should work.

您可以在要用于提交的按钮上触发点击事件。感觉很奇怪,但它应该有效。

回答by david

as Mathletics suggested, fire the submit via onclick

正如 Mathletics 建议的那样,通过 onclick 触发提交

<form name="oh" action="/">
<select onchange="oWhichSubmit(this)" onkeypress="oWhichSubmit(this)">
<option value=""></option><option value="submit1">Submit1</option><option value="submit2">Submit2</option>
</select>
<input type="submit" name="submit1" id="submit1" value="Submit 1" />
<input type="submit" name="submit2" id="submit2" value="Submit 2" />
</form>

gets the selected option's value used to referencing the submit element

获取用于引用提交元素的选定选项的值

 <script>
function oWhichSubmit(thisSubmit){
  var oWhich = thisSubmit.value;   
     if(document.getElementById(oWhich)){
      document.getElementById(oWhich).click();
    }
}
</script>

回答by Rulian Gutiérrez

this is another form that can be used

这是可以使用的另一种形式

<select class="form-control" id="search" name="search" value="search" onchange="this.form.submit()">
  <option>1</option>  
    <option>2</option>  
    <option>3</option>  
     
  </select>