我想在任何其他onsubmit操作之前执行验证。不幸的是,我无法控制表单上onsubmit属性的值。例如:
<form id="myForm" onsubmit="return stuffICantChange()"></form>
我已尝试过以下代码和其他几种方法,但没有运气:
$("#myForm").onsubmit = function() {
console.log("hi");
}
感谢任何帮助,谢谢。
如果这是重复的,请在标记之前告诉我,以便我可以在必要时驳回索赔。
编辑:
我要求的代码:
<form id="form_ContactUs1" name="form" method="post" action="index.php" onsubmit="return Validator1(this) && ajaxFormSubmit(this); return false">
<div class="form">
<div class="form-staticText">
<p>We look forward to hearing from you! Please fill out the form below and we will get back with you as soon as possible.</p>
</div>
<div class="form-group">
<input class="form-control" placeholder="Name" id="IDFormField1_Name_0" name="formField_Name" value="" size="25" required="" type="text">
<span class="form-control-feedback"></span>
</div>
<div class="form-group">
<input class="form-control" placeholder="Email" id="IDFormField1_Email_0" name="formField_Email" value="" size="25" required="" type="email">
<span class="form-control-feedback"></span>
</div>
<div class="form-group">
<input class="form-control bfh-phone" data-format="ddd ddd-dddd" placeholder="Phone" id="IDFormField1_Phone_0" name="formField_Phone" value="" size="25" type="tel">
<span class="form-control-feedback"></span>
</div>
<div class="form-group">
<textarea class="form-control" placeholder="Comments" name="formField_Comments" id="IDFormField1_Comments_0" cols="60" rows="5" required=""></textarea>
<span class="form-control-feedback"></span>
</div>
<div class="row submit-section">
<input name="submit" class="btn btn-success submit-button" value="Submit" type="submit">
</div>
</div>
$( "form" ).each(function() {
console.log( $(this)[0] );
sCurrentOnSubmit = $(this)[0].onsubmit;
$(this)[0].onsubmit = null;
console.log( $(this)[0] );
$( this )[0].onsubmit( function() {
console.log( 'test' );
});
});
答案 0 :(得分:0)
除了已经执行的函数之外,你应该能够不引人注意地将另一个onsubmit
函数添加到#myForm
:
function myFunction() {
...
}
var myForm = document.getElementById('myForm');
myForm.addEventListener('submit',myFunction,false);
答案 1 :(得分:0)
尝试
$("#myForm").submit(function(){
.. Your stuff..
console.log("submit");
return false;
});
每次提交表单时都会触发,然后结束返回false会停止表单默认操作继续。
答案 2 :(得分:0)
试试这个,这很简单Javascript:
function overrideFunction(){
console.log('Overrided!');
}
var form;
form = document.querySelector('#myForm');
form.setAttribute('onsubmit','overrideFunction()');
问候。
答案 3 :(得分:0)
您应该在表单中的每个字段上触发更改事件以检查验证。
$('input').on('change', function(e) {
if($(this).val() == '') {
console.log('empty');
}
});
这将帮助用户更快地等待提交。
您还可以在提交前尝试点击事件。
$('#formsubmitbutton').on('click', function(e) {
//your before submit logic
$('#form').trigger('customSubmit');
});
$('#form').on('customSubmit', function(e) {
//your normal submit
});
答案 4 :(得分:0)
尝试以下代码:
$("#myForm").on('submit',function() {
console.log("hi");
});