2017-02-20 83 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()