使用jQuery将复选框添加到无序列表

时间:2009-08-18 12:05:01

标签: javascript jquery html

为无序列表中的每个<li>元素添加一个复选框并处理其回调以获取li文本或复选框属性(如name / value)的jQuery代码是什么?

我从这样的事情开始:

$(document).ready(function() {
   $("#SuperTextbox1_Results").children('li').each(function() {
      $(this).prepend('<input type="checkbox" name="test" value="test" />');
   });
});

1 个答案:

答案 0 :(得分:6)

// checkbox click event handler
$('input:checkbox.liChk').live('click', function () {
    // access parent li, do whatever you want with it
    console.log($(this).parent('li'));
});    

// append checkboxes to all li tags
$('<input type="checkbox" class="liChk" />').appendTo('li');