0

これはselectを使用したドロップダウンで、IDはbeh_typeです

<select class="form-control" id="beh_type">
<option value="0">Auto</option>
<option value="1">Man</option>
</select>

これは私の複数選択リストで、IDはmult_typeです

<select multiple="multiple" class="form-control" id="mult_type">
<option value="0">Auto</option>
<option value="1">Man</option>
</select>

これは、if条件を使用したシングルクリックのJavaScriptコードと、beh_typeではなくmult_typeに対して実行されるコードです

if($("select").attr("id") == "mult_type"){        
$("select").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()});
4

2 に答える 2

2

値を指定selectしてそのイベントが必要な場合は、セレクターを使用してそれらのイベントを追加します。idmult_typeid

$("#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()});
于 2018-08-09T11:33:12.127 に答える
1

「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()});
于 2018-08-09T11:33:42.490 に答える