gpt4 book ai didi

jquery - "Select/Unselect"多选字段

转载 作者:行者123 更新时间:2023-12-03 23:02:05 25 4
gpt4 key购买 nike

我在表列中定义了多个选择字段,

<table border=1>
<tr>
<td>THIS IS THE PLACE TO SELECT THE PROJECTS</td>
</tr>
<tr>
<td id="select_project">
<select class="projects" id="projects" multiple="multiple" name="projects[]" size="10">
<option value="1">Project 1</option>
<option value="2" selected="selected">project 2</option>
<option value="3">Project 3</option>
</select>
CLICK HERE SHOULD "UNSELECT"
</td>
</tr>
</table>

我希望有一个功能,当用户单击选择字段之外的列的空间时,所有选定的选项应返回到未选定状态,我使用以下jquery来实现,

$("#select_project").bind('click', function(){
$('#projects option').attr('selected', false);
return false;
});

它可以工作,但它也会影响选择字段,即当用户选择一个选项时,它立即自动变回未选择状态,如何摆脱这种情况?

最佳答案

嗯,是的,您将事件绑定(bind)到整个表格单元格,其中选择框和文本所在,因此它会影响它们两者。固定代码:

<table border=1>
<tr>
<td>THIS IS THE PLACE TO SELECT THE PROJECTS</td>
</tr>
<tr>
<td id="select_project">
<select class="projects" id="projects" multiple="multiple" name="projects[]" size="10">
<option value="1">Project 1</option>
<option value="2" selected="selected">project 2</option>
<option value="3">Project 3</option>
</select>
<a href="#" id="unselect">CLICK HERE SHOULD "UNSELECT"</a>
</td>
</tr>
</table>

$("#unselect").bind('click', function(){
$('#projects option:selected').removeAttr("selected");;
return false;
});

关于jquery - "Select/Unselect"多选字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4803334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com