如何将JavaScript应用于1表单中的2个表单中的1个提交按钮? [英] How to apply javascript to only 1 of 2 form submit buttons within 1 form?
问题描述
您好我正在使用此脚本来防止用户提交特定的空白表单文本输入字段。做得很好,但我在2个表单中有2个提交按钮,我需要这个工作只有1个。有什么办法让下面的代码适用于使用按钮ID或名称的1个特定按钮吗?
< script type =text / javascript> $('form')。on('submit',function(){
var thisForm = $(this);
var thisAlert = thisForm.data('alert');
var canSubmit = true;
thisForm.find('[data-alert]')。each(function(i){
var thisInput = $(this);
if !$ trim(thisInput.val())){
thisAlert + ='\ n'+ thisInput.data('alert');
canSubmit = false;
};
});
if(!canSubmit){
alert(thisAlert);
return false;
}
});
< / script>
您有一个表单和两个提交按钮,默认,都会提交表单。为了防止一个按钮被提交,添加一个单击处理程序,既可以防止默认的提交操作,也可以执行任何您希望该按钮执行的操作。
$ b HTML strong>
< form id =form1>
< input type =textvalue =something/>
< input id =submit1type =submitvalue =send/>
< input id =submit2type =submitvalue =ignore/>
< / form>
JavaScript 演示 https://jsfiddle.net/BenjaminRay/ccuem4yy/ Hi I'm using this script to prevent users from submitting specific blank form text input fields. Does a great job but I have 2 submit buttons within 1 form and I need this to work for only 1. Is there any way to make this code below apply to 1 specific button using the button id or name?
You have one form and two submit buttons which, by default, will both submit the form. To prevent one button from submitting, add a click handler that both prevents the default submit action and does whatever else you want that button to do. HTML JavaScript Demo https://jsfiddle.net/BenjaminRay/ccuem4yy/ 这篇关于如何将JavaScript应用于1表单中的2个表单中的1个提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< $('#c $ c> $('#submit2')。on('click',function(event){
event.preventDefault();
// Form will not be提交
//当点击这个按钮时,做任何你需要做的事
});
$ b $('form').on('submit',function(){
var thisForm = $(this);
var thisAlert = thisForm。 data('alert');
var canSubmit = true;
thisForm.find('[data - alert]')。each(function(i){
var thisInput = $(this );
if(!$。trim(thisInput.val())){
thisAlert + ='\ n'+ thisInput.data('alert');
canSubmit = false ;
};
});
if(!canSubmit){
alert(thisAlert);
return false;
}
} );
<script type="text/javascript">
$('form').on('submit', function () {
var thisForm = $(this);
var thisAlert = thisForm.data('alert');
var canSubmit = true;
thisForm.find('[data-alert]').each(function(i) {
var thisInput = $(this);
if ( !$.trim(thisInput.val()) ) {
thisAlert += '\n' + thisInput.data('alert');
canSubmit = false;
};
});
if( !canSubmit ) {
alert( thisAlert );
return false;
}
});
</script>
<form id="form1">
<input type="text" value="something" />
<input id="submit1" type="submit" value="send" />
<input id="submit2" type="submit" value="ignore" />
</form>
$('#submit2').on('click', function (event) {
event.preventDefault();
// Form will not be submitted
// Do whatever you need to do when this button is clicked
});
$('form ').on('submit ', function () {
var thisForm = $(this);
var thisAlert = thisForm.data('alert');
var canSubmit = true;
thisForm.find(' [data - alert]').each(function (i) {
var thisInput = $(this);
if (!$.trim(thisInput.val())) {
thisAlert += '\n ' + thisInput.data('alert ');
canSubmit = false;
};
});
if (!canSubmit) {
alert(thisAlert);
return false;
}
});