2014-12-19 62 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"); 
}); 
相关问题