我做了一个表格并将其attr设置为onsubmit="return false"
现在我需要在实际提交之前执行一些操作,如下所示:
$('input[name=submit]').click(function(){
$('.switch-input').attr('checked','checked');//getting all values of chbz
//here goes a little more code//
$('form[name=setform]').removeAttr('onsubmit');//finally sending to $_POST
});
乍一看一切都正常,但我不知道它会在发送之前100%执行所有代码还是我做错了?
onsubmit="return false"
属性将阻止表格像通常那样被提交。但是,当用户单击“提交”输入时,上面的代码段将执行一次,删除onsubmit
属性并使表单能够正常提交,但用户必须再次单击“提交”才能提交。
您可以保留现有的代码片段,并通过.submit()
方法的第三个变体告诉jQuery明确提交表单:
$('input[name=submit]').click(function(){
$('.switch-input').attr('checked','checked');//getting all values of chbz
//here goes a little more code//
$('form[name=setform]').removeAttr('onsubmit');//make the form submittable
$('form[name=setform]').submit();//finally sending to $_POST
});
或者,您可以删除onsubmit="return false"
属性,并使用方法的.submit(handler)
变体,这简化了代码并保证处理程序将在表单提交之前执行:
$('form[name=setform]').submit(function(){
$('.switch-input').attr('checked','checked');//getting all values of chbz
//here goes a little more code//
});
您可以考虑忽略onsubmit
属性,而是使用您的.click()
代码,最后只返回false。你也可以按照Stiliyan的建议使用.submit()
。