最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onchange="this.form.submit()" versus two submit buttons - Stack Overflow

programmeradmin0浏览0评论

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?

<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>

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

<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.

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?

<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>

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

<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.

Share Improve this question edited Nov 29, 2011 at 20:16 Exit asked Nov 29, 2011 at 18:35 ExitExit 9933 gold badges13 silver badges28 bronze badges 1
  • 1 My english ir very poor and I understand that you some of your submit doesn't works? Is that right ?? – user898741 Commented Nov 29, 2011 at 18:46
Add a comment  | 

4 Answers 4

Reset to default 6

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>

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

-edit-

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

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

as Mathletics suggested, fire the submit via 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>
发布评论

评论列表(0)

  1. 暂无评论