簡體   English   中英

如何在不按控制鍵的情況下從select中獲取多個選項?

[英]how to get multiple option from select without pressing control key?

我在我的html代碼中有2'選擇',從第一個選擇,我想只選擇一個選項,從第二個選擇我想選擇多個選項。 我在jquery中編寫代碼以從select中獲取多個選項。

JQuery的

$('option').mousedown(function(e) {
        e.preventDefault();
        $(this).prop('selected', !$(this).prop('selected'));
        return false;
    });

這個代碼是完美的,但問題是我的第二個選擇工作正常,但在我的第一個選擇我不能選擇任何選項,我只想從第一個選擇只獲得一個選項。

HTML

        <div class="form-group">
            <div class="col-sm-3">
                <label style="width: 100%;" class="control-label"
                    th:text="#{CLONE.MASTERID}"></label>
            </div>
            <div class="col-sm-9">
                <select class="form-control" id="cloneMasterIds">
                    <option value="">Select Master Device Id</option>
                    <option th:each="master : ${masterIds}" th:value="${master.value}"
                        th:utext="${master.value}">Wireframe</option>
                </select>
                <p class="field-error hidden-true"
                    id="SerialNumber-Error" th:text="#{ERROR.CLONE.MASTERID}"></p>
            </div>
        </div>

        <!-- Child IDs -->
        <div class="form-group">
            <div class="col-sm-3">
                <label style="width: 100%;" class="control-label"
                    th:text="#{CLONE.CHILDID}"></label>
            </div>
            <div class="col-sm-9">

                <select class="form-control select-checkbox" id="cloneChildIds"
                    multiple="multiple">
                    <option th:each="child : ${childIds}" th:value="${child.value}"
                        th:utext="${child.value}">Wireframe</option>
                </select>
                <p class="field-error hidden-true"
                    id="SerialNumber-Error" th:text="#{ERROR.CLONE.CHILDID}"></p>
            </div>
        </div>

按id選擇第二個select語句。 處理mousedownmousemove事件:

$("#cloneChildIds").mousedown(function(e){
    e.preventDefault();

    var select = this;
    var scroll = select.scrollTop;

    e.target.selected = !e.target.selected;

    setTimeout(() => select.scrollTop = scroll, 0);
    $(select).focus();
}).mousemove(e => e.preventDefault());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM