How to Trigger a function inside a JQuery file jquery.js from a html <form>
Lets say This is my html form definition:
<form id="feedback" action="" enctype="mul开发者_如何学Pythontipart/form-data" method="post">
The jQuery file is defined in the head tag:
<script type="text/javascript" src="js/ajaxarchive.js"></script>
I've also created a html div within an input field as a button:
<div id ="button" class="button">
<input type="submit" name="submit" id="submit" value="Enviar" />
</div>
I have JavaScript code inside my index.html and I prefer keep the code to validate my form outside the index.htm. I could add my functions in a sentence like this (but I rather want to call my functions from an external file):
if ($li_e.attr('class')==='cc_content_13'){
/*here I try to recieve code from the ajaxarchive.js inside functions*/
}
How I define the functions also inside the ajaxarchive.js in order to to something like
var formData = $('form').serialize();
submitForm(formData); <-- This is the name of the function
It sounds like you want to hook up some validation logic to the clicking of the "Submit" button without modifying the actual HTML to include a reference. If that's the case then just bind to the click
event in an external js file
$(document).ready(function() {
$('#submit').click(function() {
// Call your function here.
});
});
You can bind a function to your form's "submit" event. You can define your function anywhere you wish, as long as you include its source file before calling $.ready.
// somewhere in $(document).ready:
$("#feedback").submit(yourExternalFunctionNameHere);
Where parameter e in the function is the event object. Make sure to call e.preventDefault() or return false inside the function so the form won't submit by default - you can submit it manually by using:
$("#feedback").submit();
精彩评论