Dynamically send requests using Jqeury control form

Source: Internet
Author: User

When the Submit button is used in 1.jquery, the form will still be submitted when return false

The workaround is to use the button buttons instead of the Submit button to see the following example:

<form id= "form" action= "back.action" method= "get" >

<input type= "text" id= "Backselect" value=${sessionscope.teachername}/>
<input type= "Submit" id= "Backselect" value= "opt-out"/>
</form>

I'm going to decide if I'm sure to opt out before sending the request to Back.action.

jquery is as follows:

JQuery (' #backSelect '). Click (function () {
var date=new date ();
var a = new Date ("06/21/2015 12:00");
var B = new Date ("06/23/2015 12:00");
if (date>=a&&date<=b) {
Jconfirm (' Are you sure you want to opt out? ', ' Confirm box ', function (r) {
var message = r = = true? ' Yes ': ' No ';
if (r==true) {
jQuery (' #form '). Submit ();//This control sends a submit request
}else{
Jalert (' Please fill out the argumentative form ');
}
});
}else{
Jalert ("Time has past, you cannot opt out");
}
});

------------------------------above is not able to solve the problem, when you click, will send a submit request, this is probably the difference between the Submit and button--------------------

2. You can use this article to learn how jquery submits a form

(2) write the following code, which will create a form that contains an input button (not a Submit button). Add some jquery code that will trigger and submit the form when the button is clicked.

<form id= "form" action= "back.action" method= "Get" >

<input type= "text" id= "Backselect" value=${sessionscope.teachername}/> <input type= "Submit" id= "ba Ckselect "value="/> </form>

--------------------------jquery----------------------------

JQuery (' #backSelect '). Click (function () {var date=new date ();var a = new Date ("06/21/2015 12:00");var B = new Date ("06/23/2015 12:00");if (date>=a&&date<=b) {Jconfirm (' Are you sure you want to opt out? ', ' Confirm box ', function (r) {var message = r = = true? ' Yes ': ' No ';if (r==true) {jQuery (' #form '). Submit ();//This control sends a submit request}else{Jalert (' Please fill out the argumentative form ');}});}else{Jalert ("Time has past, you cannot opt out");}
        });

Just change the type inside the form to a button to easily solve the problem, control when you decide to send the request, so that you can dynamically send the request-


1.5.3 principle

In this example, the Click event handler is appended to the input button. The event handler is executed when the button is clicked. When you click the button, the Submit () method of jquery is called on the form, which submits the form. All browsers have a native submission method that is used to submit the form programmatically. jquery wraps this functionality into its own submit () method.

1.5.4 More information

Control form Submission

If the form has a submit button, we can control whether or not to submit the form. In this case, you must attach an event handler to the form that will be executed when you click the Submit button on a specific form.

$ (' #Form '). Submit (function () {return false;});

The code above will be executed when you click the Submit button on a form with ID. If the processing function returns FALSE, the form is not submitted. This is very convenient for validating the forms. The code that validates the form value can be placed in the handler function. If the form value passes validation, returns True to submit the form. When validation fails, returns false, and the form is not allowed to be submitted.

Another option is to use Preventdefault (). As can be seen from the name, Preventdefault () prevents the execution of the default event. It is a genus of the event object

$ (' #Form '). Submit (Function (event) {Event.preventdefault ()});

Dynamically send requests using Jqeury control form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.