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;"/>