2009-09-19 25 views
11

選擇複選框,我有以下的HTML:jQuery的:基於名稱和值

<form id="test"> 
    <input type="radio" value="A" name="C1"/> 
    <a href="javascript:selectCheckbox('C1', 'A');"> OPTION-A </a> 

    <input type="radio" value="B" name="C1"/> 
    <a href="javascript:selectCheckbox('C1', 'B');"> OPTION-B </a> 

<input type="radio" value="C" name="C1"/> 
    <a href="javascript:selectCheckbox('C1', 'C');"> OPTION-C </a> 

    // several other: C2, C3, .. 
</form> 

而且我想實現selectCheckbox(chkbox, value),這應該:

  1. 搜索所有無線電與name = chkbox和設置attr('checked') = false
  2. 搜索收音機有name = chkbox AND val() = value和設置attr('checked') = true

我想不通,正確的選擇是什麼,我想沒有任何運氣如下:

var name = "#" + chkbox + " :checked"; 
$(name).each(.. // doesn't work 

$('#'+chkbox).each(.. // if finds only the first occurence 
         // of i.e. C1, although I would expect 3 

$("input[@name='"+chkbox+"']").each(function() { .. 
// leaves me with the following error: 
// Warning: Expected attribute name or namespace but found '@name' 

請讓我知道我做錯了。非常感謝!

+0

的代碼你不應該有相同的ID(「C1」)的多個元素 – Zed 2009-09-19 17:13:35

+0

你說得對,我刪除了ID現在。謝謝! – MrG 2009-09-19 17:15:41

+2

「注意:在jQuery 1.3 [@attr]樣式選擇器被刪除(他們以前在jQuery 1.2中被棄用)。「 – Zed 2009-09-19 17:17:19

回答

4

我會使用相對DOM位置做導航。請注意,不能使用相同的id具有不同的元素,但在這種情況下,您不需要使用名稱上的屬性選擇器來查找正確的輸入。請注意,您已經知道需要根據點擊鏈接的位置檢查各種輸入中的哪一個。我更喜歡將代碼與標記分開,所以我給鏈接一個類,以使它們易於選擇並將點擊處理程序應用程序移入代碼中。

更新:請注意,我刪除了代碼以取消選中其他無線電。對於收音機,設置一個要檢查的應該會自動取消選中其他的。

$(function() { 
    $('a.checkbox-selector').click(function() { 
     // get the checkbox immediately preceding the link -- this should 
     // be checked. Checking it should automatically uncheck any other 
     // that may be checked. 
     $(this).prev(':checkbox'); 
       .attr('checked',true); 
     return false; // don't process the link 
    }); 
}); 

<form id="test"> 
    <input type="radio" value="A" name="C1"/> 
    <a href="#" class="checkbox-selector"> OPTION-A </a> 

    <input type="radio" value="B" name="C1"/> 
    <a href="#" class="checkbox-selector"> OPTION-B </a> 

    <input type="radio" value="C" name="C1"/> 
    <a href="#" class="checkbox-selector"> OPTION-C </a> 

    <!-- several other: C2, C3, ... --> 
</form> 
0

嘗試$("input[name='C1'] :checked]")。 ID應該是唯一的,所以jQuery可能不希望返回多個元素。

1

在一個頁面上不能有多個具有相同ID的元素。您所有的輸入元素都有ID「C1」。

+1

你可以這樣做,但由於幾個原因,這是一件壞事,試試看,並注意$(」#id「 )和$(「[id = id])。後者將返回一個包含但不少元素共享id「id」的集合,而前者將返回一個包含僅包含具有此id的第一個聲明元素的包裝集合。 – 2009-09-19 17:38:21

7

感謝您的幫助,我終於明白了:

function selectTestAnswer(chkbox, value) { 
    $("[name="+chkbox+"]").each(function() { 
     if ($(this).val() == value) 
      $(this).attr('checked', true); 
     else 
      if ($(this).attr('checked') == true) 
       $(this).attr('checked', false); 
}); 

}

+0

我認爲這是比你需要更多的代碼。 – tvanfosson 2009-09-19 17:34:15

22

試試這個:

$('input:radio[name="' + chkboxName + '"][value="' + value + '"]') 
    .attr('checked', 'checked'); 
5

您可以使用道具()代替ATTR()如果你的jquery版本> 1.6

請參閱此鏈接 http://api.jquery.com/prop/

在jQuery 1.6中,.prop()方法提供了一種明確 檢索屬性值,同時.attr()檢索屬性。

所以,你正在尋找看起來更像

$('input:checkbox[name="Validators"][value="' + v + '"]').prop('checked',true);