简体   繁体   中英

How to avoid changing event when a item was selected in Jquery?

I'm create selection to select data from DB but set ID="myselect" for selector

But this ajax will call if I have selected an items and change to another items i want to call ajax only one times when user click on its items.

$(document).on("click", "#myselect", function (e) {

    $.ajax({
        url: '/teller/assignTo',
        dataType: 'json',
        Method: 'get',
        success: function (data, status) {

            var userId = data.users;
            $.each(userId, function (key, value) {
                $('#myselect').append($("<option></option>").attr("value", key).text(value.name));
            });
        },
        error: function () {
            throw new Error("Could not load  " + data);
        }
    });
});

HTML

<select name="assign_user_id" id="myselect" class="form-control" ></select>

Try this

HTML

<select name="assign_user_id" id="myselect" class="form-control not-clicked" ></select>

JQuery

$(document).on("click", "#myselect", function (e) {
    if( $(this).hasClass( "not-clicked" ) ) {
        $.ajax({
            url: '/teller/assignTo',
            dataType: 'json',
            Method: 'get',
            success: function (data, status) {    
                var userId = data.users;
                $.each(userId, function (key, value) {
                    $('#myselect').append($("<option></option>").attr("value", key).text(value.name));
                });
            },
            error: function () {
                throw new Error("Could not load  " + data);
            }
        });
        $(this).removeClass("not-clicked");
    }
});

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