admin管理员组文章数量:1332361
I have a form with two buttons. The bottom one should submit the form to the server. In the middle of the form there is another button that needs to trigger a simple scoring function and add this score value in a new form field (which will or won't be submitted depending on its existence).
Using the Prototype js library, how would I cancel this middle button from submitting the form and have it perform my calculation/DOM addition function?
EDIT:
What I have currently isn't preventing the form submit...
$$('.form-submit-calculate').each(function(calculator){
calculator.observe("click",function(){alert('calculate'); return false; });
});
for
<div id="cid_31" class="form-input-wide">
<div style="margin-left:406px" class="form-buttons-wrapper">
<button id="input_31" class="form-submit-calculate">
Get Score
</button>
</div>
</div>
I have a form with two buttons. The bottom one should submit the form to the server. In the middle of the form there is another button that needs to trigger a simple scoring function and add this score value in a new form field (which will or won't be submitted depending on its existence).
Using the Prototype js library, how would I cancel this middle button from submitting the form and have it perform my calculation/DOM addition function?
EDIT:
What I have currently isn't preventing the form submit...
$$('.form-submit-calculate').each(function(calculator){
calculator.observe("click",function(){alert('calculate'); return false; });
});
for
<div id="cid_31" class="form-input-wide">
<div style="margin-left:406px" class="form-buttons-wrapper">
<button id="input_31" class="form-submit-calculate">
Get Score
</button>
</div>
</div>
Share
Improve this question
edited Jul 20, 2019 at 16:07
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Jun 23, 2011 at 18:34
two7s_clashtwo7s_clash
5,8371 gold badge30 silver badges45 bronze badges
0
4 Answers
Reset to default 7Event.observe('scoreButton', 'click', function(e) {
Event.stop(e);
}
});
Your button
does not need to automatically submit your form.
Give it a type="button"
and nothing will happen. There is no default action with type="button"
. You set the action then in your JS.
Read more: http://reference.sitepoint./html/button
simply have the function which is attached to the click event of the button you don't want to submit return false. you can also use e.preventDefault within the event function.
This seems to do the trick for me:
$$('.form-submit-calculate').each(function(calculator){
calculator.onclick = function(){
alert('calculate');
return false;
}
});
Feel free to tell me your better more elegant answer! (And why it is... I'm just hacking my way around Prototype right now, no idea what the best practices are.)
本文标签: javascriptUsing Prototype how do I prevent a button from submitting a formStack Overflow
版权声明:本文标题:javascript - Using Prototype how do I prevent a button from submitting a form? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742319100a2452418.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论