简体   繁体   中英

Select Menu Resets to Previously Selected Option

I have a select menu that fires an ajax post then displays a success or failure message on :success.

The HTML for the menu looks like so:

<select name="return_action_id" class="form-control">
    <option value="0"> --- Please Select --- </option>
    <option value="1" selected>Refunded</option>
    <option value="2">Credit Issued</option>
    <option value="3">Replacement Sent</option>
</select>

The Javascript for the ajax post looks like so:

$('select[name="return_action_id"]').change(function(){
    var a=$(this);
    var value = a.val();
    $.ajax({
        url:'index.php?route=sale/returns/action&token=<?= $token; ?>&return_id=<?= $return_id; ?>',
        type:'post',
        dataType:'json',
        data:'return_action_id='+value,
        beforeSend:function(){
            a.blur().button('loading').append($('<i>',{class:'icon-loading'}));
        },
        complete:function(){
            a.button('reset');
        },
        success:function(json){
            if(json['error']){
                alertMessage('danger',json['error']);
            }
            if(json['success']){
                alertMessage('success',json['success']);
                //$('select[name="return_action_id"] option:selected').prop('selected', false);
                //$('select[name="return_action_id"] option[value="'+value+'"]').prop('selected', true);
            }
        }
    });
});

Every time I select an option from the menu, the Ajax fires and creates my message as expected, but the option selected does not receive the selected property and the menu reverts back to the originally selected option.

For instance in the html code above, Refunded is currently selected. If I select Credit Issued in the menu, the Ajax fires and the menu reverts back to having Refunded selected.

You'll see in the javascript success method I have some code commented out where I tried to make it behave.

Any help would be appreciated.

您在此处重置select菜单a.button('reset') ,删除此行,当前值将保持选中状态。

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