為radio類型的INPUT添加客戶端腳本(附加實現(xiàn)JS來禁用onClick事件思路代碼)
更新時間:2010年11月11日 15:21:50 作者:
為radio類型的INPUT添加客戶端腳本(附加實現(xiàn)JS來禁用onClick事件思路代碼),需要的朋友可以參考下。
下面的例子將展示其結(jié)果是沒有重載顯示提交。
當用戶選擇一個選項上面,一個函數(shù)叫做“getVote()”執(zhí)行。該功能所引發(fā)的“的OnClick”事件
<html>
<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)
判斷控件的disabled屬性是不是true,是的話return false;實現(xiàn)禁用radio的onclick事件并可再次啟用它
方法一:(同時實現(xiàn)禁用,重新啟用功能,只能針對button text類型的INPUT,對div無法禁用其onclick事件)
<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('cmd1').disabled=true;" />
<br/>
方法二,三:(實現(xiàn)移除radio的onclick事件,需再次重新注冊事件,可以禁用div的onclick事件)
<input type="button" value="Click me to disable the onclick event on first button" onclick="document.getElementById('cmd1').onclick=function(){};" />
<br/>
三:
<input type="button" value="Click me to disable the onclick event on first button" onclick="document.getElementById('cmd1').onclick=null;" />
當用戶選擇一個選項上面,一個函數(shù)叫做“getVote()”執(zhí)行。該功能所引發(fā)的“的OnClick”事件
復制代碼 代碼如下:
<html>
<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)
判斷控件的disabled屬性是不是true,是的話return false;實現(xiàn)禁用radio的onclick事件并可再次啟用它
方法一:(同時實現(xiàn)禁用,重新啟用功能,只能針對button text類型的INPUT,對div無法禁用其onclick事件)
<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('cmd1').disabled=true;" />
<br/>
方法二,三:(實現(xiàn)移除radio的onclick事件,需再次重新注冊事件,可以禁用div的onclick事件)
<input type="button" value="Click me to disable the onclick event on first button" onclick="document.getElementById('cmd1').onclick=function(){};" />
<br/>
三:
<input type="button" value="Click me to disable the onclick event on first button" onclick="document.getElementById('cmd1').onclick=null;" />
您可能感興趣的文章:
- 怎么通過onclick事件獲取js函數(shù)返回值(代碼少)
- javascript 動態(tài)改變onclick事件觸發(fā)函數(shù)代碼
- 詳解js的事件處理函數(shù)和動態(tài)創(chuàng)建html標記方法
- JavaScript利用發(fā)布訂閱模式編寫事件監(jiān)聽函數(shù)
- ES6中javascript實現(xiàn)函數(shù)綁定及類的事件綁定功能詳解
- JavaScript觸發(fā)onScroll事件的函數(shù)節(jié)流詳解
- 深入理解Node.js 事件循環(huán)和回調(diào)函數(shù)
- 實例講解javascript注冊事件處理函數(shù)
- js動態(tài)添加input按鈕并給按鈕增加onclick的函數(shù)事件(帶參數(shù))完整實例
相關(guān)文章
javascript寫的一個表單動態(tài)輸入提示的代碼
javascript寫的一個表單動態(tài)輸入提示的代碼...2007-08-08實現(xiàn)一個年、月、季度聯(lián)動SELECT的javascript代碼
實現(xiàn)一個年、月、季度聯(lián)動SELECT的javascript代碼...2007-07-07在textarea中顯示html頁面的javascript代碼
在textarea中顯示html頁面的javascript代碼...2007-04-04js+css使文本框自動適應(yīng)內(nèi)容的高度
使文本框自動適應(yīng)內(nèi)容的高度的實現(xiàn)代碼,需要的朋友可以參考下。2010-05-05javascript各瀏覽器中option元素的表現(xiàn)差異
javascript各瀏覽器中option元素的表現(xiàn)差異,需要的朋友可以參考下。2011-04-04比較簡單的javascript實現(xiàn)input雙擊后可以編輯
比較簡單的javascript實現(xiàn)input雙擊后可以編輯...2007-08-08使用button標簽,實現(xiàn)三態(tài)圖片按鈕
使用button標簽,實現(xiàn)三態(tài)圖片按鈕...2007-01-01