jquery小问题我需要帮助

时间:2010-05-20 23:23:25

标签: jquery

您好我需要一些jquery的帮助,我在点击它们旁边的复选框时重命名下拉列表。我想在下面的代码中获取名为“Prev”的下拉列表的选定选项值,并分配给单击的复选框。我希望这是有道理的。感谢

$('.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 个答案:

答案 0 :(得分: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');
    }

});

我无法确定您是否想要将复选框的值分配给选择列表,反之亦然。我选择“将复选框的名称设置为选择列表的值”。希望这就是你所追求的。

相关问题