简体   繁体   English

如果恢复选择框选项

[英]Reverting selectbox option back if

So i've a situation where I have a selectbox that makes a jquery .post call to another script on a .change event. 所以我的情况是我有一个selectbox,它在.change事件上调用另一个脚本的jquery .post。

I was wondering if there is anyway i can revert the selectedoption back to the original default should something occurs or that my .post event fail? 我想知道无论如何,如果发生某事或我的.post事件失败,我可以将选择的选项恢复为原始默认值吗?

Hi ppl, I'm sorry. 嗨,我很抱歉。

Here's the javascript code. 这是javascript代码。 I've made some amendments to it. 我已经对它做了一些修改。 I'm trying to get the second line to work as this code is not working now due to some errors(due to don't know what on the second line). 我正试图让第二行工作,因为这些代码由于一些错误而无法正常工作(由于不知道第二行是什么)。 My selectbox is part of a list so i'm not able to make it a unique id to it and to remember the box the user selects, i need to use $(this). 我的选择框是列表的一部分,所以我不能使它成为一个唯一的ID,并记住用户选择的框,我需要使用$(this)。

    $('.order_stateselect').click(function(){
        var val=$(this).find(":selected").val();
        $(this).change(function(){
            if(confirm('Confirm Order Status Change?')){
                var conduct_status_chge=$.post('/seller/helpers/order_status_change.php',{order_item_id:$(this).parent('td').siblings('.order_item_id').text(),order_item_status:$(this).find(':selected').val()},function(data){
                    alert(data);
                })
                .error(function() { alert("error"); })
            }
        });
    });

Difficult to tell without code, but I'll try 没有代码很难说,但我会试试

$("#selectbox").data('origValue', $("#selectbox").val());

$("#selectbox").on('change', function () {
   $.post(...).done(function () {
      // other stuff
      $("#selectbox").data('origValue', $("#selectbox").val());
   }).fail(function () {
      $("#selectbox").val($("#selectbox").data('origValue'));
   });
});

Use something like this 使用这样的东西

 var value=$("#selectbox").val();
    $.ajax({
    type:post,
    url:"something.com",
    success:function(){

    },error:function(){
    $("#selectbox").val(value);
    }

    })
$('select').val('defaultvalhere');

您可以使用它来选择不关于默认值的第一个选项:

$('#selectId option:eq(0)').attr('selected',true);

you can simply use a global variable to save the option selected previously. 您可以简单地使用全局变量来保存先前选择的选项。 Once save you can just use that on your error Its something like this 一旦保存,您可以在error上使用它就像这样

<script type="text/javascript">
this.previousVal = 0;
function changeHandler(selectBox)
    {    

$.ajax({
type:post,
url:"URL",
success:function(){
//ur code
this.previousVal=selectBox.selectedIndex;
},error:function(){
//use previousVal and set the value back to the previous value
}

})


    }                           
</script>

<select id="changeLang" name="changeLang" onchange="changeHandler(this)"></select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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