2010-05-20 115 views
4

嗨,我需要一点jQuery的帮助,我重命名下拉列表旁边的复选框被点击。我想在下面的代码中获取名为'Prev'的下拉列表中的选定值,并将其分配给点击的复选框。我希望这是有道理的。谢谢jquery小问题我需要帮助

$('.mutuallyexclusive').live("click", function() { 


      checkedState = $(this).attr('checked'); 
      $('.mutuallyexclusive:checked').each(function() { 
       $(this).attr('checked', false); 
       $(this).attr('name', 'chk'); 
      }); 
      $(this).attr('checked', checkedState); 

      if (checkedState) { 
       jQuery('#myForm select[name=cat.parent_id]').attr('name', 'bar') 

       // here is the bit i need help with 
       // get the selected option of the dropdown prev and set it to $(this).val.. something along those lines 
       var prev = $(this).prev('select').attr("name", 'cat.parent_id'); 

      } 
      else { 
       var prev = $(this).prev('select').attr("name", 'dd'); 
      } 

     }); 
    }); 
+1

你有你正在使用的表单项的HTML标记吗? – EvilChookie 2010-05-20 23:26:01

+0

我很难理解你想要做什么。获取选定选项$(“select option:selected”)的值。val(); – 2010-05-20 23:30:25

+0

是,从选择列表'var Prev'并将该值设置为已选中的复选框 – Martin 2010-05-20 23:52:03

回答

1

HTML结构将帮助吨,但第一个优化是缓存您的初始复选框。接下来,利用jQuery中的隐含迭代。如果我理解你正在试图做什么,我结束了这一点:

$('.mutuallyexclusive').live("click", function() { 

    var $check = $(this); 

    var checkedState = $check.attr('checked'); 

    $('.mutuallyexclusive:checked') 
     .attr('checked', '') 
     .attr('name', 'chk'); 

    $check.attr('checked', checkedState); 

    if (checkedState) { 
     $check.attr('name', $check.prev('select').val()); 
    } else { 
     $check.attr('name', 'dd'); 
    } 

}); 

我不能肯定地告诉从你的问题,你是否想复选框的值赋给选择列表,或副反之亦然。我去“将复选框的名称设置为选择列表的值”。希望这就是你以后的样子。