开发者

Two Submit Buttons Issue --

I have two submit buttons in one form and both the submit are linked to one text box. Is there a way to select a specific "submit" button when enter(on Keyboard) is pressed.

my html:

<form id="myForm" method="post">
id: 
<input type="text" name="id" id="id"/>
<div id="hidden" style="display: none;">
<label>Age:</label>
<input type="text" name="Age"/><br/>
<input type="submit" id="button2" value="Update"/>
&开发者_开发知识库lt;/div> 
<input type="submit" id="button1" value ="Get Info" onclick="document.getElementById('hidden').style.display = ''; this.style.display = 'none'"/> 

Small Explanation of the code: Initially all the part will be hidden except one submit("Get Info") and id field. After user enters id and click submit("Get Info") all other fields will appear and the previous submit("Get Info") will be hidden.


Without getting into the JavaScript necessary to make this happen, the submit button that a form uses is defined in the spec.

Essentially the form will use the first enabled submit button that it contains for implicit submission.

Really, what it sounds like you want to do is add an onsubmit handler to the form and prevent the default action (so you can show the rest of the form). In the event handler, you simply need to remove the event handler from the form so that the subsequent call to submit the form will be sent correctly.


You can use the .click() method on the button you want clicked.

document.forms.myForm.id.onkeyup = function (e) {
    e = e || window.event;
    if (e.keyCode == 13) {
        document.forms.myForm.button1.click();
        if (e.preventDefault) {
            e.preventDefault();
        }
        e.returnValue = false;
    }
};


I solved a different problem in essentially the same manner. Here is, using jQuery, a simple solution

$('form').bind('submit',function(e){ 
    e.preventDefault(); 
    //do whatever processing you need to, then fire your Get Info button
    $('#buttonToTrigger').trigger('click') 
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜