2014-08-27 129 views
1

我已經搜索並再次搜索解決方案,但找不到任何類似的案例。jQuery:查找與其他名稱的複選框是否具有相同的值

一個表裏面,我有一個複選框PHP的形式,這樣產生的:

while($getalldefenseurs=mysqli_fetch_assoc($result_check_alldefenseurs)){ 
     echo ' 
     <tr class="liste-defenseurs"> 
      <td>'.$getalldefenseurs['Calciatore'].'&nbsp;('.$getalldefenseurs['Squadra'].')</td> 
      <td class="number"><input type="checkbox" class="bouton" value="'.$getalldefenseurs['id'].'" name="titulaire-defenseur"/></td> 
      <td class="number"><input type="checkbox" class="bouton" value="'.$getalldefenseurs['id'].'" name="remplacant-defenseur"/></td> 
     </tr>'; 
    } 

它給了我這樣的事情:

<tr class="liste-defenseurs"> 
     <td>LICHTSTEINER&nbsp;(JUVENTUS)</td> 
     <td class="number"><input type="checkbox" name="titulaire-defenseur" value="220" id="LICHTSTEINER" class="bouton"></td> 
     <td class="number"><input type="checkbox" name="remplacant-defenseur" value="220" class="bouton"></td> 
     <td class="number"><input type="checkbox" name="reserviste-defenseur" value="220" class="bouton"></td> 
    </tr> 
    <tr class="liste-defenseurs"> 
     <td>CEPPITELLI&nbsp;(CAGLIARI)</td> 
     <td class="number"><input type="checkbox" name="titulaire-defenseur" value="355" id="CEPPITELLI" class="bouton"></td> 
     <td class="number"><input type="checkbox" name="remplacant-defenseur" value="355" class="bouton"></td> 
     <td class="number"><input type="checkbox" name="reserviste-defenseur" value="355" class="bouton"></td> 
    </tr> 

正如你所看到的,我不能知道每個複選框的價值,但我知道他們的名字。

事情是我不希望任何具有相同值的複選框在同一時間被檢查。 所以在我的javascript我試過下面這段代碼,但它並不總是工作,我認爲這是由於我檢查框在我的表...的順序

$("tr.liste-defenseurs td input:checkbox").on('change',function(e) {  
    if($("input[name='remplacant-defenseur']:checked").val() == $("input[name='titulaire-defenseur']:checked").val()) { 
     $(this).prop('checked', false); 
     alert("You can't check 2 checkboxes on the same line at the same time."); 
    } 
}); 

誰能幫我糾正我的jQuery代碼爲它工作嗎?

非常感謝

回答

0

的事情是我不希望在同一時間進行檢查具有相同價值的複選框。

所以不管該複選框,如果複選框被選中,要取消所有其他有同樣的名字?您可以在value選擇:

$("tr.liste-defenseurs td input:checkbox").on('change',function(e) { 
    $('input[type=checkbox][value="' + this.value + '"]') 
     .not(this) 
     .prop("checked", false); 
}); 

邊注:當您使用jQuery僞選擇像,你強迫jQuery來處理處理JavaScript代碼的選擇,而不是換手至瀏覽器的更有效自己選擇器處理。有時這是值得的,但我會使用input[type=checkbox],而不是,因爲後者真的不會買任何東西...

+1

非常感謝這個快速簡單的解決方案,它的作用就像一個魅力!你的建議幫助了我。 – Fanzouze 2014-08-27 11:26:58

0

我不是很習慣stackoverflow - 但我看不到名稱='remplacant-在輸出表中引用了'attaquant']和name ='titulaire-attaquant']。

你可以嘗試的另一種方法是簡單地檢查$(this).val()對選中的變量數組。

var selected_groups = $("input[name='names-french']:checked").map(function() { 
     return $(this).val(); 
    }).get(); 

這將解決它 - 但我認爲這是一個命名的問題從一開始。

+0

對不起,我有一個很長的代碼,我複製了錯誤的行,我的意思是name ='remplacant-defenceur'和name ='titulaire-defenceur',我會改變它! 非常感謝您的回答! – Fanzouze 2014-08-27 11:17:42

0
$("tr.liste-defenseurs td input:checkbox").on('change',function(e) {  
    if($('input[value="'+$(this).val()+'"]:checked').length > 1){ 
    alert("You can't check 2 checkboxes on the same line at the same time."); 
    $(this).attr('checked',false); 
    } 
}); 
+0

謝謝你的幫助:) – Fanzouze 2014-08-27 11:27:43

0
$(document).ready(function(){ 
    var array = []; 
    $('table').find('input').each(function(){ 
     $(this).on('change',function(){   if(jQuery.inArray($(this).val(), array) === -1){ 
      array.push($(this).val()); 
      $(this).prop("checked",true); 
      }else{ 
       alert("You cant check!!"); 
       $(this).prop("checked",false); 
      } 
     }) 
    }) 
}); 

這工作對我很好!

相關問題