jQuery function on form input
I want users to enter something in an text field, press OK and then run a function on the value of that text.
I have been looking at开发者_开发技巧 jQuery docs, I found keyup which gets the value instantly (not what I want) and the only things I can find about doing stuff with submitted form data involve POSTing the results to a php file or something.
I am keen to learn so if you could post links to resources where I could find out more along with your answer I would be most grateful. Thanks.
You don't need a <form>
for this (if you're not sending any data anywhere), just a set of <input>
elements will do, for example:
<input id="myInput" type="text" />
<input id="myButton" type="button" value="Click me!" />
And jQuery like this:
$("#myButton").click(function() {
myFunction($("#myInput").val());
});
You can test it out here, all we're doing is attaching a handler to the button via .click()
and getting the value from the textbox via .val()
...and then using that however you want, in this case passing it as a parameter to the function.
If you want to work on the value of the form, without submitting the form:
$('form#formId').submit(
function() {
var inputText = $('input#inputText').val();
//do something with inputText
return false; // prevent the form being submitted to the server
}
);
A link to the jQuery docs for forms.
You can catch the submit with the jQuery submit() function
<form id="target" action="destination.html">
<input type="text" value="Hello there" />
<input type="submit" value="Go" />
</form>
<script>
$('#target').submit(function() {
alert('Handler for .submit() called.');
return false;
});
</script>
if your ok button is submit button of a form then you are looking for a jQuery/javascript onsubmit event it happen when the submit button is clicked. On this event you can trigger a function which does your calculations.
<form action='' onsubmit='abc()'>
function abc()
{
......
}
http://api.jquery.com/submit/
精彩评论