2014-07-11 36 views
0

通過下面我試圖禁用兩個sRadio1輸入或如果其中任何檢查兩個sRadio2輸入顯示的HTML,所以我使用JQuery這樣做嘗試,如下圖所示,但由於某種原因,它不工作。有人可以告訴我,我在這裏做什麼錯誤,以及如何解決它?由於JQuery的使用.find禁用無線電輸入元素

 <ul class="soptionslist"> 
      <li> 
      <h2>Options</h2> 
      <ul class="soptions"> 

       <li> 
       <fieldset> 
        <input type="radio" class="sRadio" name="sRadio1"> 
       </fieldset> 
       <fieldset class="option2"> 
        <input type="radio" class="sRadio" name="sRadio1"> 
       </fieldset> 
       <p>Option A</p> 
       </li> 

       <li> 
       <fieldset> 
        <input type="radio" class="sRadio" name="sRadio2"> 
       </fieldset> 
       <fieldset class="option2"> 
        <input type="radio" class="sRadio" name="sRadio2"> 
       </fieldset> 
       <p>Option B</p> 
       </li>      
      </ul> 
      </li> 
      </ul> 

jQuery代碼

$(".soptions input").change(function() { 
    if (this.checked) { 
     $(this).parent('li').find('input').attr('disabled', true); 
    } else { 
     $(this).parent('li').find('input').attr('disabled', true); 
    } 
    }); 

回答

1

使用closest()而不是parent()

改成這樣:

$(".soptions input").change(function() { 
    if (this.checked) { 
     $(this).closest('li').find('input').prop('disabled', true); 
    } else { 
     $(this).closest('li').find('input').prop('disabled', true); 
    } 
}); 

兼容性:

jQuery的1.6+:

使用.prop()功能:

$(this).closest('li').find('input').prop('disabled', true); 

的jQuery 1.5及以下:

.prop()功能不可用,所以你需要使用.attr()

$(this).closest('li').find('input').attr('disabled', true); 

JSFiddle

0

Fiddle

你需要使用.prop()代替.attr()

$(".soptions input").change(function() { 
    if (this.checked) { 
     $(this).parent('li').find('input').prop('disabled', true); 
    } else { 
     $(this).parent('li').find('input').prop('disabled', true); 
    } 
    }); 

.prop()reference

相關問題