2012-10-17 28 views
0

所以我有一個情況,我有一個選擇框,使一個.change事件的另一個腳本的jQuery .post調用。恢復selectbox選項返回如果

我想知道是否有反正我可以恢復selectedoption回到原來的默認值,如果發生或我的.post事件失敗?

嗨,我很抱歉。

這裏是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"); }) 
      } 
     }); 
    }); 
+3

可能有,但沒有代碼我們應該怎麼知道?您可以至少發佈選擇列表和更改事件的代碼! – adeneo

回答

0

使用這樣的

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

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

    }) 
+0

這個答案應該適合你。您可以使用$(this)來檢索您的原始元素,並從那裏恢復爲默認值。 –

+0

@ShawnWernig ok ...查看編輯答案 – StaticVariable

0
$('select').val('defaultvalhere'); 
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')); 
    }); 
}); 
0

你可以用它來選擇默認關於第一個選項不價值:

$('#selectId option:eq(0)').attr('selected',true); 
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>