2017-02-20 53 views
2

我有2个按钮yesno,只是我正在切换它们。单选按钮检查属性没有设置为假

<!DOCTYPE html> 
    <html> 
     <head> 
     <title>Demo Project</title> 
     </head> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
     <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
     <script src=""></script> 
     <script> 
      function enableCheckBoxAndDisableNoRadioButton() { 
      $(".checkbox").attr("checked",true); 
      $("#no").attr("checked", false); 
      } 

      function disableCheckBoxAnddisableYesRadioButton() { 
      $("#yes").attr("checked", false); 
      $(".checkbox").attr("checked",false); 
      } 
     </script> 
     <body> 
     <input class="checkbox" type="checkbox" > 
     <p>yes <input id="yes" type="radio" onchange="enableCheckBoxAndDisableNoRadioButton()"></p> 
     <p> no<input id="no" type="radio" checked=true onchange="disableCheckBoxAnddisableYesRadioButton()"></p> 
     </body> 
    </html> 

当我点击yes,那时没有按钮被取消选择,并且复选框被选中。 但是当我点击no按钮时,那个时间复选框被取消选中,但是yes按钮也被选中。我想让它取消选择。

让我知道是什么问题。

我在chrome上运行这段代码:53.0。

回答

1

代替attr,我将它更改为.prop,它现在正在工作。

5

使用prop insted attr

function enableCheckBoxAndDisableNoRadioButton() { 
    $(".checkbox").prop("checked", true); 
    $("#no").prop("checked", false); 
    } 

    function disableCheckBoxAnddisableYesRadioButton() { 
    $("#yes").prop("checked", false); 
    $(".checkbox").prop("checked", false); 

    } 

Working Fiddle

0

变化.attr.prop那么它的工作

0

你需要检查,我们可以使用.prop()/.attr()

使用.prop()而不是.attr()在大多数情况下。

请参见下面的属性列表中的一些财产:

| **Attribute/Property** | **Attribute** | Property 

| accesskey    |  Yes  | No 
| align     |  Yes  | No 
| async     |  Yes  | Yes 
| autofocus    |  Yes  | Yes 
| checked    |  Yes  | Yes 
| class     |  Yes  | No 
| href     |  Yes  | No 
| multiple    |  Yes  | Yes 
| readonly    |  Yes  | Yes 

,并了解更多关于.prop() vs .attr()

相关问题