Javascript 为什么在多选列表中未第二次选择该选项?

Javascript 为什么在多选列表中未第二次选择该选项?,javascript,html,Javascript,Html,我需要通过JavaScript在multi-select中选择取消选择选项。代码看起来不错,但在您第二次尝试选择同一项时不会触发 因此,尝试选择全职,再次单击它,将取消选择。再次尝试单击它,它将无法工作 $'.multi-select option'.在“鼠标向下”上,函数e{ e、 防止违约; 选择top=$this.parent.scrollTop; var before=$this.propselected; console.warnbefore:+before; if before==

我需要通过JavaScript在multi-select中选择取消选择选项。代码看起来不错,但在您第二次尝试选择同一项时不会触发

因此,尝试选择全职,再次单击它,将取消选择。再次尝试单击它,它将无法工作

$'.multi-select option'.在“鼠标向下”上,函数e{ e、 防止违约; 选择top=$this.parent.scrollTop; var before=$this.propselected; console.warnbefore:+before; if before==真{ $this.propselected,false; } 否则{ $this.attrselected,selected; } console.info从+before+更改为+this.prop'selected'; mustChangeScrollTop=true; 返回true; }; 多选择jquery测试 选择所有适用的选项 全职的 兼职的 每日津贴 特嫩蝗
如果我完全理解你的意思,那么这应该是你想要的,只需要一点小小的改变

$'.multi-select option'.在“鼠标向下”上,函数e{ e、 防止违约; 选择top=$this.parent.scrollTop; var before=$this.propselected; console.warnbefore:+before; if before==真{ $this.propselected,false; } 否则{ $this.propselected,true;//更改在这里 } console.info从+before+更改为+this.prop'selected'; mustChangeScrollTop=true; 返回true; }; 多选择jquery测试 选择所有适用的选项 全职的 兼职的 每日津贴 特嫩蝗
如果我完全理解你的意思,那么这应该是你想要的,只需要一点小小的改变

$'.multi-select option'.在“鼠标向下”上,函数e{ e、 防止违约; 选择top=$this.parent.scrollTop; var before=$this.propselected; console.warnbefore:+before; if before==真{ $this.propselected,false; } 否则{ $this.propselected,true;//更改在这里 } console.info从+before+更改为+this.prop'selected'; mustChangeScrollTop=true; 返回true; }; 多选择jquery测试 选择所有适用的选项 全职的 兼职的 每日津贴 特嫩蝗
问题很小,;else语句更改.attr值而不是.prop值

属性和属性虽然相似,但引用的基础值不同。以下是工作解决方案,在else语句中将.attr更改为.prop:

$'.multi-select option'.在“鼠标向下”上,函数e{ e、 防止违约; 选择top=$this.parent.scrollTop; var before=$this.propselected; console.warnbefore:+before; if before==真{ $this.propselected,false; } 否则{ $this.propselected,selected; } console.info从+before+更改为+this.prop'selected'; mustChangeScrollTop=true; 返回true; }; 多选择jquery测试 选择所有适用的选项 全职的 兼职的 每日津贴 特嫩蝗
问题很小,;else语句更改.attr值而不是.prop值

属性和属性虽然相似,但引用的基础值不同。以下是工作解决方案,在else语句中将.attr更改为.prop:

$'.multi-select option'.在“鼠标向下”上,函数e{ e、 防止违约; 选择top=$this.parent.scrollTop; var before=$this.propselected; console.warnbefore:+before; if before==真{ $this.propselected,false; } 否则{ $this.propselected,selected; } console.info从+before+更改为+this.prop'selected'; mustChangeScrollTop=true; 返回true; }; 多选择jquery测试 选择所有适用的选项 全职的 兼职的 每日津贴 特嫩蝗
else {
    $(this).attr("selected", "selected");
}