2011-09-22 164 views
0

時checkbox1選擇兩個複選框標記1將獲得選擇和背景將被刪除複選框,取消選擇和選擇

here is the fiddle

對於例如,如果我選擇checkbox1應該選擇兩個複選框標記checkbox1我要的是

+1

你是怎麼長的花格式化這個問題? <10秒? – Matt

+0

我以爲我用Word格式寫的問題會保持格式化 – June

+1

幾乎從來沒有從文字處理器中剪切並粘貼到需要純文本的東西上。堅持純文本,並使用網站提供的格式:) :) –

回答

3

問題是,搜索ID $(「#somethin」)是不可能選擇多個元素的。如果您搜索類,那麼你的榜樣工程..良好,some minor changes :)

$(document).ready(function() { 
    $('input[type=checkbox]').change(function(){ 
     var pos; 
     pos = this.id; 

     // global hook - unblock UI when ajax request completes 
     $(document).ajaxStop($.unblockUI); 


     if($(this).prop("checked")) { 
      $("."+pos).parent().removeClass("highlight"); 
      $("."+pos).prop('checked', true) 

      //ajax to add uni 
     } else { 
      //ajax to remove uni 
      $("."+pos).parent().addClass("highlight"); 
      $("."+pos).prop('checked', false) 


     } 
    }); 

}); 
+0

同意...您的意見確實幫助我找出我的問題+1,接受的答案 – June

+2

(你的是比我更通用的,+1 :) –

0

你不能有相同ID的多個DOM元素。

查看this fiddle作爲一個工作示例,但它不像您可能需要的那樣通用。

0

使用class選擇多個元素。你可以試試這個..

<script language="javascript" type="text/javascript"> 
    $(function() { 
     $("#checkbox1").click(function() { 
      $('.case').attr('checked', this.checked); 
     }); 
    }); 
</script> 

和你的HTML代碼就會像下面

<div> 
    <table> 
<tr> 
    <td><input type="checkbox" id="checkbox1" class="highlight"/></td> 
    <td>Item1</td> 
    <td>value</td> 
</tr> 
<tr> 
    <td align="center"><input type="checkbox" class="case" name="case" value="1"/></td> 
    <td>Item2</td> 
    <td>value</td> 
</tr> 

</table> 
    </div> 
相關問題