如何使用JQuery迭代枚举的Check List(复选框)?

时间:2011-08-30 06:30:25

标签: javascript jquery javascript-events checkbox

如何使用JQuery迭代枚举的Check List(复选框)?

您好,我有以下检查清单(包含2个复选框,其中包含2个不同的电子邮件地址):

<div id="emailCheckListId" class="checkList">
 <ul id="emailCheckListId_ul">
    <li>
        <label for="mycomponent.emailCheckList_0" class="checkListLabel">
            <input type="checkbox"  
                   value="johndoe@yahoo.com" 
                   id="mycomponent.emailCheckList_0" 
                   name="mycomponent.emailCheckList"/>
                   johndoe@yahoo.com
        </label>
    </li>
    <li>
        <label for="mycomponent.emailCheckList_1" class="checkListLabel">
            <input type="checkbox"  
                   value="johndoe@gmail.com" 
                   id="mycomponent.emailCheckList_1" 
                   name="mycomponent.emailCheckList"/> 
                   johndoe@gmail.com
        </label>
    </li>
</ul>

每次用户点击单个复选框时,都可以使用JavaScript事件监听器填充/删除文本字段:

// Event listener which picks individual contacts
// and populates input field.
$('#emailCheckListId_ul input:checkbox').change(function() {
     // Declare array
     var emails = [];

     // Iterate through each array and put email addresses into array
     $('#emailCheckListId_ul input:checkbox:checked').each(function() {
         emails.push($(this).val());
     });

     // Assign variable as To: text field by obtaining element's id.
     var textField = document.getElementById("mycomponent.textfield");

     // Add / Remove array from text field
     textField.value = emails;
 });

但是,我现在有一个枚举的检查列表...只需要追加/连接索引号(我存储在隐藏字段中,并且总是可以获得 纠正一个),但似乎无法看到如何传递第二个迭代器的“输入:复选框:已选中”。

以下是选中列表的HTML源代码:

 <div id="mycomponent.comment0.replyToRecipientsCheckList" 
      class="checkList" 
      onclick="selectIndividualRecipients()">
      <ul id="mycomponentcomment0.replyToRecipientsCheckList_ul">
          <li>
            <label for="mycomponent.comment0.replyToRecipientsCheckList_0" 
                 class="checkListLabel">
               <input type="checkbox"  
                      value="johndoe@yahoo.com" 
                      id="mycomponent.comment0.replyToRecipientsCheckList_0" 
                      name="mycomponent.comment0.replyToRecipientsCheckList"/>
                      johndoe@yahoo.com
            </label>
    </li>
    <li>
        <label for="mycomponent.comment0.replyToRecipientsCheckList_1" 
               class="checkListLabel">
               <input type="checkbox"  
                      value="johndoe@gmail.com" 
                      id="mycomponent.comment0.replyToRecipientsCheckList_1" 
                      name="mycomponent.comment0.replyToRecipientsCheckList"/> 
                      johndoe@gmail.com
        </label>
    </li>
 </ul>
</div>

JavaScript的:

// Uses an event listener which picks individual contacts
// and populates input field.
function selectIndividualRecipients() {

    var checkListPrefix = 'mycomponent.comment';    
    var index = document.getElementById("commentHiddenField").value;
    var checkListPostfix = '.replyToRecipientsCheckList_ul';
    var event =  checkListPrefix + index + checkListPostfix;

    $(event).change(function() {

         // Declare array
         var emails = [];

         // Iterate through each array and put email addresses into array

         // THIS DOESN'T SEEM TO BE WORKING:
         var test = '#' + event.attr('id') + 'input:check:checked';
         $(test).each(function() {
         alert('inside iterator');
         alert('this.val: ' + $(this).val());
         emails.push($(this).val());
         alert('emails = ' + emails);
    });

    // Assign variable to Reply To: text field by obtaining element's id.
    var textField = document.getElementById(indexedReplyTextField);

    // Add / Remove array from text field
    textField.value = emails;
}

浏览器说($ test).each()抛出一个未捕获的异常,这是不允许的......

我可能做错了什么?

非常感谢您阅读此内容......

2 个答案:

答案 0 :(得分:1)

您是否尝试过如下更改var测试声明?

var test = '#' + event.attr('id') + ' input:checkbox:checked';

注意输入前的空格和复选框,而不是检查。

答案 1 :(得分:1)

可能有一种更简单的方法:

$('div.checkList').delegate('input', 'change', function() {
   textField.value = $('div.checkList ul li input:checked').map(function() {
      return $(this).val();
   }).get().join('; ');
});