问题
this is a simple question. I have a form that is being validated using jquery's .validate function.
The problem is that validating only seems to work when I submit the form using: <input type='submit' />
However, I would like to use my own custom button using instead: <button type="submit">click to submit</button>
.... or use something like... <a onclick="submitFunction ()">click to submit</a>
These last two options however, do not trigger jquery's validate function. Is there a way around this? Thanks for the help in advance!
回答1:
The jQuery Validation plugin is tied to the submit event in jQuery. If you want to trigger this from anywhere else (i.e. any other event handler), you will have to call .submit()
manually, which will trigger the validation to occur.
Note that after calling .validate() to set up validation on your form you can store the Validator instance it returns and call the form method on it if you want to perform the validation separately from the submission of the form.
回答2:
As far as I know jQuery doesn't have a "validate" method, but either way, in order to use elements to submit the form just bind the submit event:
$('#formid').submit(function(){
// validate form
...
// if validation fails, return false;
});
回答3:
What you need to do in your custom submit function is as follows
jQuery('#formId').submit();
You can create your own validation and when the form is valid use the above method to submit it. This is the same as clicking a submit button in the form. #formId is the id you've given your form. There are several other ways you can reference the form as well.
回答4:
If all you want to do is change what the submit button says, you could use
<input type="submit" value="click to submit" />
Update To work with the validation plugin, this is really what I would suggest. If you want to do anything else when they click that button, you can use submitHandler as an option: http://docs.jquery.com/Plugins/Validation/validate#toptions .
来源:https://stackoverflow.com/questions/2360557/submitting-a-form-using-jquerys-validate-function