简体   繁体   English

使用 jquery 检查禁用的单选按钮

[英]Check a disabled radio button with jquery

I´m using JQUery Mobile 1.3.1 to check a disabled radio button (ie a radio button that cannot be clicked).我正在使用 JQUery Mobile 1.3.1 检查禁用的单选按钮(即无法单击的单选按钮)。 Unfortunately, my method is not able to check the radio button a disabled radio button.不幸的是,我的方法无法检查单选按钮禁用的单选按钮。 I already tried to find answers here but haven´t found anything useful yet.我已经尝试在这里找到答案,但还没有找到任何有用的东西。 This is my code:这是我的代码:

<a href="#" id="activateRadio" data-role="button">Activate radio button flug!</a>
<input type="radio" name="radio1" id="radio-choice-1" value="choice-1" disabled />

and the JQuery mobile function:和 JQuery 移动功能:

$('#activateRadio').click(function(){         
      //check if radio button is  not checked

      if ( ! $("input[name='radio3']").is(':checked') ) {
        alert("The button was not checked. Now it should be checked!"); 
        //remove disabled attribute
        $("input[name='radio3']").removeAttr('disabled');
        $("input[name='radio3']").attr('checked', 'checked');
      }

      else {
        alert("The button is already checked!");
      }
});

Another question would also be how to change the background color of a input field radio when the the radio is checked.另一个问题也是如何在选中收音机时更改输入字段收音机的背景颜色。 How does this look like in the CSS?这在 CSS 中看起来如何?

use prop() to change the property..attr just chnages the attributes使用prop()更改属性..attr 只是更改属性

  if ( ! $("input[name='radio3']").is(':checked') ) {
    alert("The button was not checked. Now it should be checked!"); 
    //remove disabled attribute
    $("input[name='radio3']").prop('disabled',false);
    $("input[name='radio3']").prop('checked', true);

    //or 
    $("input[name='radio3']").prop({'disabled':false',checked':true});

  }

  else {
    alert("The button is already checked!");
  }

and not sure if it is radio3 or radio1 since i cannot find radio3 here并且不确定它是radio3还是radio1,因为我在这里找不到radio3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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