2014-12-19 61 views
0

我有一個功能,如果用戶在選擇框中將IP更改爲未分配,則會彈出一個提示,詢問他們是否確定,取消此提醒I希望select2框改回原始狀態(即分配等)。在javascript中更改選擇框的選定文本

我已經能夠通過在變量存儲原始值,然後取消該警告

else 
    $('#ip_status').val(original_status) 

然而,儘管這並不改變其分配到選擇的值來改變選擇2框的值select2框的值意味着功能正在運行,select2框中的文本保持「未分配」,給最終用戶一個誤導性的值。請記住.text(「示例」)不起作用。

謝謝。

全碼 -

original_status = document.getElementById('ip_status').value 
$('#ip_status').change -> 

# If the user changes ip status to unallocated, clear and hide system name and description fields. 
if parseInt($('#ip_status').val()) == 0 

    r = confirm("You are about to unallocate this IP! Are you sure you want to do this?") 
    if r == true 
    $('#ip_description').val("").parent().hide() 
    $('#ip_system_name').val("").parent().hide() 

    else 
    $('#ip_status').val(original_status) 
else 
    # if the select changes to allocated or reserved, re-show the fields that were hidden 
    $('#ip_description').parent().show() 
    $('#ip_system_name').parent().show() 
+0

您可以提供您正在使用的現有代碼 – 2014-12-19 10:55:40

+0

請參閱更新代碼 – 2014-12-19 10:59:56

+0

您的問題與Coffeescript有關嗎?我看到的只是JQuery。 – 2014-12-19 11:05:07

回答

0

我能夠通過改變

$('#ip_status').val(original_status) 

解決這個問題,以

$("#ip_status").select2("val", original_status) 
0

你必須使用類似:

$(function() { 
    $("#your_option").attr("selected", "selected"); 
}); 
相關問題