简体   繁体   中英

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

I have 2 'select' in my html code and from first select, i want to choose only one option and from second select i want to choose multiple options. I have written code in jquery to get multiple options from select.

JQuery

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

and this code is working perfect, but the problem is my second select is working fine but in my first select i am not able to choose any option and i just want to get only one option from first select.

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>

Select the second select statement by id. Handle the mousedown and mousemove events:

$("#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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM