单击后如何在固定的时间间隔内禁用单选按钮?

时间:2014-09-03 12:00:54

标签: javascript html

大家好,

    <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>

我希望在单击其中一个单选按钮时禁用所有单选按钮,然后在固定的时间间隔后启用它。

5 个答案:

答案 0 :(得分:1)

如果你不介意使用jQuery,这是一个如何做到这一点的例子。您可以使用HTML属性disabled在单击时禁用无线电,然后使用setTimeout在设定的时间后再次启用它们。下面的时间设置为3000毫秒。

$('document').ready( function() {
    $('input').on('click', function(){
        $('input').attr('disabled',true);

        setTimeout(function() {
            $('input').attr('disabled',false);
        }, 3000);
    });    
});

http://jsfiddle.net/0912shz0/1/

答案 1 :(得分:1)

您可以尝试以下代码: -

$('#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

DEMO here (JSFiddle)

答案 2 :(得分:0)

您需要的是一个计时器,使用 setTimeout

https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout

在函数正文中,您需要知道如何在javascript中使用禁用属性:

https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Attribute/disabled

您不需要将 this.value 传递给该函数,您需要的是 this - 这将是您要禁用的元素,或者< strong> event - 包含 event.target 元素的事件。注意:如果您传递事件参数,则需要使用跨浏览器代码,以便它可以在不同的浏览器中使用。例如:

function(event) {
    //short-hand for: if(event) event=event; else event=window.event;
    event = event || window.event; 
    // your code here
}

答案 3 :(得分:0)

$('input[type="radio"]').click(function(){
     $(this).attr('disabled', true);

     setTimeout(function() {
          $('input[type="radio"]').attr('disabled', false);
    }, /*time interval in ms*/)
});

没有经过测试,是从头上写的

答案 4 :(得分:0)

为我工作

    <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>