1

我有一个这样的复选框项目:

 $("#userRoleDiv-"+note_id).append(" \n\
    <div><input name='userInput-"+note_id+"' type='checkbox' value='' id= '1' />Instructor</div>\n\
    <div><input name='userInput-"+note_id+"' type='checkbox' value='' id= '2' />Students</div>\n\
 ");

现在,我想检查具有 id 1 和/或 2 但名称相同的复选框,如代码中所示。怎么做到呢?

4

3 回答 3

2

你也可以试试这个(它很干净)。也不需要使用\n和注意checked:checked (只有一个)

var instructor = $('<input/>', { 'name':'userInput-'+note_id, 'id':1, 'type':'checkbox', 'checked':'checked' });
var students = $('<input/>', { 'name':'userInputs-'+note_id, 'id':2, 'type':'checkbox' });

$("#userRoleDiv-"+note_id).append(
    $('<div/>', {'text':'Instructor'}).prepend(instructor),
    $('<div/>', {'text':'Students'}).prepend(students)
);

演示。

于 2013-07-15T01:29:24.943 回答
0

给定一个名字:

var name = 'userInput-1';

选择这样的复选框:

var checkboxes = $('input[name=' + name + ']').filter('#1, #2');

然后检查它们:

checkboxes.each(function () { this.checked = true; });

请注意,我没有对此进行测试;它可能需要修改才能工作。

于 2013-07-15T01:17:58.010 回答
0

checked属性应用于<input>元素。

此外,您应该将输入包装在<label>标签中,以便标签(教师和学生)可以点击。

$("#userRoleDiv-"+note_id).append("
  <label><input name='userInput-"+note_id+"' type='checkbox' id='1' checked />Instructor</label>
  <label><input name='userInput-"+note_id+"' type='checkbox' id='2' />Students</label>
");
于 2013-07-15T00:58:03.893 回答