无法选中“选择多个”下拉列表的复选框?

时间:2019-05-01 05:46:02

标签: jquery dropdown nice

我使用的是不错的选择下拉菜单,其中有多个选项可供我选择或取消选择。

这里的问题是我可以选中或取消选中所有选项,而需要的是在页面加载时选中特定的复选框。

我尝试了很多,还添加了这些行。

$('#Person').val("1").prop('checked', true);          
$("#Person").niceSelect("update");

执行此操作时,在控制台中显示“ checked:true”,但更改不会反映在页面上。

要取消选中所有复选框,我正在使用以下代码。

$(".styled-checkbox").prop('checked', false);

<div class="box"><div class="nice-select wide"  tabindex="0" id="Person"></div></div>    

$('#Person').val("1").prop('checked', true);
$("input[type=checkbox][value=" + xy + "]").attr("checked", "true");
$("#Person").val(xy);
$('#Person').val(xy).prop('selected', true);
$('#Person').val(xy);      
$('#Person').multiSelect('select', [2, 1]);
$("#Person option[value='2']").prop('selected', true);
$("#Person option[value='2']").attr("selected", true);

如何使用其值设置选中或未选中的复选框?

2 个答案:

答案 0 :(得分:0)

您使用错误的属性选择了下拉菜单中的值,请尝试

<select id="abc">
  <option value="">Select</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>


//abc id of your select and i want to select option with value 2
<script>
  $('#abc option[value="2"]').prop("selected", "true");
  $("select").niceSelect("update");
</script>

答案 1 :(得分:0)

我通过从下面的代码中删除单词“ checked”来解决此问题。 默认情况下,它用于在其他位置加载时选中所有复选框。 如果我们要删除此单词,则可以选中或取消选中特定选项。

'<li> <input class="styled-checkbox" checked id="styled-checkbox-' + i + '" type="checkbox" value="' + data[i][dataValueField] + '"><label for="styled-checkbox-' + i + '">' + data[i][dataTextField] + '</label></li>';