开发者

Form submit and Ajax call

I am having a problem when submitting a form.

Here is the situation

<form ....>

    <input type="text" id="ajax"> <img onclick="saveTextBox">

    <input type="text">
    <input type="text">
    <input type="text">

</form>

In the above form when the img is click on I call an Ajax script to save the button. That's OK.

What I need is when I am focus on textbox Ajax call ajaxscript (without click img).

I add an jQuery to manage key=13 and call the script BUT the problem is that run the Ajax script AND submits the form too.

How can I override the form sumit when I am focus on Ajax text box and hi开发者_如何学运维t enter key?


First. Scrap the image and replace it with a real submit button.

Then. Handle all the JS interaction in the onsubmit event for the form element. Cancel the default action, so that the JS runs and the normal form submission doesn't (when JS is available).

In short: be pragmatic, build on something that works, and intercept events at the right moment. Don't ignore the primary purpose of an element and create an entirely new way to trigger something it does already.


Return false.


Sample event function that will stop the submit event:

function submitHandler() {
    // do ajax stuff here

    return false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜