![](/img/trans.png)
[英]How to disable the radio button for a fixed interval of time after clicking them
[英]How to disable the radio button for a fixed interval of time after clicking them?
嗨,大家好,
<input type="radio" value="LIKE" onclick="func(this.value)">LIKE
<br>
<input type="radio" value="OK" onclick="func(this.value)">OK
<br>
<input type="radio" value="DISLIKE" onclick="func(this.value)">DISLIKE
<br>
我想在單擊其中一個按鈕時禁用所有單選按鈕,然后在固定的時間間隔后啟用它。
這是一個示例,如果您不介意使用jQuery,該如何做。 您可以使用禁用的HTML屬性在單擊無線電時禁用它們,然后在設置的時間后使用setTimeout再次啟用它們。 下方時間設置為3000毫秒。
$('document').ready( function() {
$('input').on('click', function(){
$('input').attr('disabled',true);
setTimeout(function() {
$('input').attr('disabled',false);
}, 3000);
});
});
您可以嘗試以下代碼:
$('#like, #unlike, #dislike').click(function(){
$("#like, #unlike, #dislike").attr("disabled",true);
setTimeout(function(){
$("#like, #unlike, #dislike").attr("disabled",false);
},2000);
})
的HTML
<input type="radio" value="LIKE" id="like" >LIKE
<input type="radio" value="OK" id="unlike">OK
<input type="radio" value="DISLIKE" id="dislike">DISLIKE
您需要使用setTimeout的計時器:
https://developer.mozilla.org/zh-CN/docs/Web/API/WindowTimers.setTimeout
在函數的主體中,您將需要知道如何在javascript中使用Disabled屬性:
https://developer.mozilla.org/zh-CN/docs/Mozilla/Tech/XUL/Attribute/disabled
而你也不必THIS.VALUE傳遞給函數,你會需要的是這一點 -這將是您希望禁用的元素,或事件 -包含在event.target元素的事件。 注意:如果您傳遞事件參數,則需要使用跨瀏覽器的代碼,以便它在不同的瀏覽器中運行。 例如:
function(event) {
//short-hand for: if(event) event=event; else event=window.event;
event = event || window.event;
// your code here
}
$('input[type="radio"]').click(function(){
$(this).attr('disabled', true);
setTimeout(function() {
$('input[type="radio"]').attr('disabled', false);
}, /*time interval in ms*/)
});
未經測試,從頭寫
為我工作
<script>
function func(thisValue){
setTimeout(function() {
$('input[type=radio]').attr("disabled",true);
$('input[type=radio]').removeAttr("checked");
$('input[value='+thisValue+']').attr("disabled",false);
$('input[value='+thisValue+']').attr("checked","checked");
$('input[value='+thisValue+']').prop('checked', true);
}, 100);
}
</script>
的HTML
<label onclick="func('LIKE')" style="position:relative"><input type="radio" value="LIKE" >LIKE</label>
<label onclick="func('DISLIKE')" style="position:relative"><input type="radio" value="DISLIKE">DISLIKE</label>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.