Javascript在Select标记ID中添加If语句
这是使用select的我的下拉列表,id为beh_typeJavascript在Select标记ID中添加If语句,javascript,Javascript,这是使用select的我的下拉列表,id为beh_type <select class="form-control" id="beh_type"> <option value="0">Auto</option> <option value="1">Man</option> </select> <select multiple="multiple" class="form-control" id="mult_type"
<select class="form-control" id="beh_type">
<option value="0">Auto</option>
<option value="1">Man</option>
</select>
<select multiple="multiple" class="form-control" id="mult_type">
<option value="0">Auto</option>
<option value="1">Man</option>
</select>
如果
需要该事件,请选择id
值为mult\u type
的
,然后使用id
选择器添加这些事件:
$("#mult_type").mousedown(function(e){
e.preventDefault();
var select = this;
var scroll = select.scrollTop;
e.target.selected = !e.target.selected;
setTimeout(function(){select.scrollTop = scroll;}, 0);
$(select).focus();
}).mousemove(function(e){e.preventDefault()});
如果
需要该事件,请选择id
值为mult\u type
的
,然后使用id
选择器添加这些事件:
$("#mult_type").mousedown(function(e){
e.preventDefault();
var select = this;
var scroll = select.scrollTop;
e.target.selected = !e.target.selected;
setTimeout(function(){select.scrollTop = scroll;}, 0);
$(select).focus();
}).mousemove(function(e){e.preventDefault()});
您只需要更改“if”的位置。正确的代码应该是
$("select").mousedown(function(e){
if($("select").attr("id") == "mult_type"){
e.preventDefault();
var select = this;
var scroll = select.scrollTop;
e.target.selected = !e.target.selected;
setTimeout(function(){select.scrollTop = scroll;}, 0);
$(select).focus();}
}).mousemove(function(e){e.preventDefault()});
您只需要更改“if”的位置。正确的代码应该是
$("select").mousedown(function(e){
if($("select").attr("id") == "mult_type"){
e.preventDefault();
var select = this;
var scroll = select.scrollTop;
e.target.selected = !e.target.selected;
setTimeout(function(){select.scrollTop = scroll;}, 0);
$(select).focus();}
}).mousemove(function(e){e.preventDefault()});
这里有什么问题?JS应该只适用于多类型id,即JS代码用于在单击中选择多个项目而不是ctrl-click这里有什么问题?JS应该只适用于多类型id,即JS代码用于在单击中选择多个项目而不是ctrl-click工作很好,我可以这样做吗$(mousedown(函数(e)你甚至可以使用
$(“select#mult_type”).mousedown
来指定你想要使用的select,但这只是id也可以。毕竟,我认为OP应该了解更多关于jquery选择器的信息:很好,我可以这样做吗$(“#mult_type“,#mult#u type1”).mousedown(函数(e)您甚至可以使用$(“select#mult_type”).mousedown
来指定要使用的select,但这只是id也可以使用。毕竟,我认为OP应该了解有关jquery选择器的更多信息: