0

所以我有一种情况,我有一个选择框,可以在 .change 事件上对另一个脚本进行 jquery .post 调用。

我想知道如果发生某些事情或我的 .post 事件失败,我是否可以将 selectedoption 恢复为原始默认值?

嗨,PPL,我很抱歉。

这是javascript代码。我对它做了一些修改。我正在尝试让第二行工作,因为由于一些错误(由于不知道第二行上的内容),此代码现在无法正常工作。我的选择框是列表的一部分,因此我无法将其设置为唯一的 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"); })
            }
        });
    });
4

5 回答 5

2

没有代码很难说,但我会尝试

$("#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'));
   });
});
于 2012-10-17T15:20:12.050 回答
0

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

$('#selectId option:eq(0)').attr('selected',true);
于 2012-10-17T15:20:24.823 回答
0

使用这样的东西

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

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

    })
于 2012-10-17T15:18:21.493 回答
0
$('select').val('defaultvalhere');
于 2012-10-17T15:18:30.883 回答
0

您可以简单地使用全局变量来保存之前选择的选项。保存后,您可以在您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>
于 2012-10-17T15:26:26.170 回答