Add client scripts for radio-type INPUT (add JS implementation to disable onClick event idea code)

Source: Internet
Author: User

The following example shows that the result is no reload display and submission.
When you select an option, a function is called "getVote ()" for execution. "OnClick" event caused by this function Copy codeThe Code is as follows: <Head>
<Script type = "text/javascript">
Function getVote (int)
{
If (window. XMLHttpRequest)
{// Code for IE7 +, Firefox, Chrome, Opera, Safari
Xmlhttp = new XMLHttpRequest ();
}
Else
{// Code for IE6, IE5
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Xmlhttp. onreadystatechange = function ()
{
If (xmlhttp. readyState = 4 & xmlhttp. status = 200)
{
Document. getElementById ("poll"). innerHTML = xmlhttp. responseText;
}
}
Xmlhttp. open ("GET", "poll_vote.php? Vote = "+ int, true );
Xmlhttp. send ();
}
</Script>
</Head>
<Body>

<Div id = "poll">
<H3> Do you like PHP and AJAX so far? </H3>
<Form>
Yes:
<Input type = "radio" name = "vote" value = "0" onclick = "getVote (this. value)"/>
<Br/> No:
<Input type = "radio" name = "vote" value = "1" onclick = "getVote (this. value)"/>
</Form>
</Div>
</Body>
</Html>

The getVote () function does the following:
Create an XMLHttpRequest object
Create the function to be executed when the server response is ready
Send the request off to a file on the server
Notice that a parameter (vote) is added to the URL (with the value of the yes or no option)
Determines whether the disabled attribute of the control is true. If yes, return false; disables the onclick event of radio and enables it again.
Method 1: Disable and re-enable the function at the same time. The function can only be used for button text INPUT, and The onclick event cannot be disabled for the div)
<Input type = "button" value = "A button. click me to see the alert box. "onclick =" alert ('I am clicked. '); "id =" cmd1 "/>
<Br/>
<Input type = "button" value = "Click me to disable the first button" onclick = "document. getElementById ('cm1'). disabled = true;"/>
<Br/>
Method 2 and 3: (to remove the onclick event of radio, you must register the event again to disable the onclick event of div)
<Input type = "button" value = "Click me to disable the onclick event on first button" onclick = "document. getElementById ('cm1 '). onclick = function () {}; "/>

<Br/>
III:
<Input type = "button" value = "Click me to disable the onclick event on first button" onclick = "document. getElementById ('cm1'). onclick = null;"/>

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.