简体   繁体   English

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

[英]How to disable the radio button for a fixed interval of time after clicking them?

Hi guys, 嗨,大家好,

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

I want to disable all the radio buttons when one of it is clicked and then to enable it after a fixed interval of time. 我想在单击其中一个按钮时禁用所有单选按钮,然后在固定的时间间隔后启用它。

Here is an example how you could do it if you don't mind using jQuery. 这是一个示例,如果您不介意使用jQuery,该如何做。 You can use the HTML attribute disabled to disable the radios when they are clicked and then use setTimeout to enable them again after a set amount of time. 您可以使用禁用的HTML属性在单击无线电时禁用它们,然后在设置的时间后使用setTimeout再次启用它们。 Below time is set to 3000 milliseconds. 下方时间设置为3000毫秒。

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

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

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

You could try code like:- 您可以尝试以下代码:

$('#like, #unlike, #dislike').click(function(){
        $("#like, #unlike, #dislike").attr("disabled",true);

   setTimeout(function(){
        $("#like, #unlike, #dislike").attr("disabled",false);
   },2000);
})

HTML 的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) 这里的演示(JSFiddle)

What you need is a timer, using setTimeout : 您需要使用setTimeout的计时器:

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

And in the body of the function you would need to know how to use the disabled attribute in javascript: 在函数的主体中,您将需要知道如何在javascript中使用Disabled属性:

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

And you would not need to pass this.value to the function, what you would need is this - which would be the element you want to disable, or event - the event which contains the element at event.target . 而你也不必THIS.VALUE传递给函数,你会需要的是这一点 -这将是您希望禁用的元素,或事件 -包含在event.target元素的事件。 Note: if you pass the event parameter you would need to use cross-browser code so that it works in different browsers. 注意:如果您传递事件参数,则需要使用跨浏览器的代码,以便它在不同的浏览器中运行。 For example: 例如:

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*/)
});

not tested, written from head 未经测试,从头写

Working for me 为我工作

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM