【问题标题】:Set selected option in dynamically populated drop down Jquery在动态填充的下拉 Jquery 中设置选定的选项
【发布时间】:2014-07-30 18:26:05
【问题描述】:

我有 2 个级联下拉菜单:下拉 A(从数据库中填充,没有静态值)和下拉 B

用户从下拉 A 中选择一个值,并基于该值填充到下拉 B 中(再次从本地存储的基于数据的表中)。这一切都很好

现在让我们说下拉 A 有项目:Item1,Item 2,Item 3....

Drop Down B 具有项目 1 的值:10,20,30

对于第 2 项:10,50,40

对于第 3 项:20,70,90。

用户选择项目 1 并从 Down B 中选择值“10”,页面提交,对于新条目,他选择项目 2,

现在我的要求是,如果用户选择项目 2,那么我的下拉 B 应该自动默认为值“!0”,因为这是用户选择的最后一个值,也是项目 2 存在的值。

我正在像这样动态创建两个下拉菜单:

function some (){

    var numUnits = measureSet.length;
    var htmlOutput = '<select name="select-choice-a_point" class="ui-select" id="select-choice-a_point" data-native-menu="false"><option>Select a Fluid Type</option>';
    for ( var i = 0; i < numUnits; i++) {

        htmlOutput += '<option value="' + measureSet[i] + '">' + measureSet[i]
        + '</option>';
    }
    htmlOutput += '</select>';

    $("#select-choice-a_point-button").remove();
    $("#select-choice-a_point").remove();
    $("#pointlabel").after(htmlOutput);
    $("#select-choice-a_point").selectmenu();

}

有人可以建议我如何将值设置为动态创建和填充的下拉列表中最后选择的值?


我已经尝试过了,它没有工作,它仍然显示默认值“选择流体类型”,请参阅下面的代码

  function abc(){


   var setLength = measureSet.length;
   var valueSet = false;
    alert(prevFuelType);
     for ( var i = 0; i < setLength; i++) {

            if(prevFuelType == measureSet[i]){

        alert("inside condition");
        valueSet =  true;
        break;
    }
}
var htmlOutput = '<select name="select-choice-a_point" class="ui-select"  id="select-choice-a_point" data-native-menu="false"><option>Select a Fluid Type</option>';


var numUnits = measureSet.length;

for ( var i = 0; i < numUnits; i++) {


    htmlOutput += '<option value="' + measureSet[i] + '">' + measureSet[i]
    + '</option>';
}
htmlOutput += '</select>';

$("#select-choice-a_point-button").remove();
$("#select-choice-a_point").remove();
$("#pointlabel").after(htmlOutput);
$("#select-choice-a_point").selectmenu();
if(valueSet == true){
    alert("bout to set");
    //$("select[id$='select-choice-a_point'] option:selected").removeAttr("selected");
    //$("select[id$='select-choice-a_point'] option[value="+ prevFuelType + ']").attr("selected","selected");

        /*  $('#select-choice-a_point option:[text="' + prevFuelType + '"]').attr('selected', true);
    $('#select-choice-a_point option:[value="' + prevFuelType + '"]').attr('selected', true);*/
    $("#select-choice-a_point").val(prevFuelType);
    $("#select-choice-a_point").text(prevFuelType);
}



$('#measurepointfield').show();
}

【问题讨论】:

    标签: jquery jquery-ui-selectmenu


    【解决方案1】:
    // drop_down_A changes. What happens now?
    var last_val = $('#drop_down_B').val(); // let's keep the current value;
    // now, you do your magic and repopulate $('#drop_down_B')
    // ..
    // ..
    $('#drop_down_B').val(last_val); 
    // if the value exists it will be selected, otherwise nothing will happen
    

    【讨论】:

    • 我自己解决了这个问题并使用了 $('#select-choice-a_point').val(prevFuelType).selectmenu('refresh');
    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2021-07-16
    • 2016-08-19
    • 2022-01-02
    • 2011-12-26
    • 1970-01-01
    相关资源
    最近更新 更多