简体   繁体   English

如何使已添加到选择标签的新选项成为jQuery中选择的

[英]how to make the new option that has been added to a select tag as selected in Jquery

I have a Jquery script where I am doing an ajax call and the value that I get in return I am using it to add it in the select option , Drop down menu , 我有一个Jquery脚本,我在其中执行ajax调用,并且得到的返回值是我使用它将它添加到选择选项,下拉菜单,

My Problem is that its not showing as selected , 我的问题是它没有显示为选中状态,

The JS is as follows JS如下

(function($, window) {
      $.fn.replaceOptions = function(options) {
        var self, $option;

        this.empty();
        self = this;

        $.each(options, function(index, option){
          $option = $("<option></option>")
            .attr("value", option.value)
            .text(option.text);
          self.append($option);
        });
        this.prop('disabled', false);
      };
    })(jQuery, window);

$(document).on('click', ".add1", function () {
    var val1 = $(".selectLevel", $(this).parents("tr")).eq(0).val(),
        val2 = $(".selectLevel", $(this).parents("tr")).eq(1).val(),
        url = "http://www.xxxyz.com/xxx/xxx/web/ajax.php",
        val3 = $(this).closest('tr').find('input').val(),
        closeSelect = $(this).closest('td').find('select');

    if (val3 == '' || val3 == null) alert('You need to add value in the input box');
    else {
        var posting = $.post(url, {
            im_core: 'selectAjaxUpdate',
            geo_level1: val1,
            geo_level2: val2,
            geo_level3: val3,
            pais: <? php echo $_POST['pais'] ?>
        }).done(function (data) {
            var obj = $.parseJSON(data);
            $.each(obj, function (key, value) {
                myArray.push({
                    text: obj[key].value,
                    value: obj[key].id
                });
            });
            //$(closeSelect).html('myArray');
            $(closeSelect).replaceOptions(myArray);
        });
    }
});

Can any one help me , With this 谁能帮我这个

Thanks in Advance 提前致谢

The problem in 问题在

$(closeSelect).replaceOptions(myArray);

try change to 尝试更改为

closeSelect.replaceOptions(myArray);

And if you use replaceoptions method from here , it not support setting selected value,because it only take array of objects and appent them to select 而且,如果您从此处使用replaceoptions方法,则它不支持设置所选值,因为它仅采用对象数组并将其附加到

You are changing source of dropdown but you are not setting new value once you source gets change. 您正在更改下拉列表的来源,但是一旦更改来源,就不会设置新值。 You just need to set selected vaulue. 您只需要设置选定的值即可。

$(document).on('click', ".add1", function () {
    var val1 = $(".selectLevel", $(this).parents("tr")).eq(0).val(),
        val2 = $(".selectLevel", $(this).parents("tr")).eq(1).val(),
        url = "http://www.xxxyz.com/xxx/xxx/web/ajax.php",
        val3 = $(this).closest('tr').find('input').val(),
        closeSelect = $(this).closest('td').find('select');

    if (val3 == '' || val3 == null) alert('You need to add value in the input box');
    else {
        var posting = $.post(url, {
            im_core: 'selectAjaxUpdate',
            geo_level1: val1,
            geo_level2: val2,
            geo_level3: val3,
            pais: <? php echo $_POST['pais'] ?>
        }).done(function (data) {
            var obj = $.parseJSON(data);
            $.each(obj, function (key, value) {
                myArray.push({
                    text: obj[key].value,
                    value: obj[key].id
                });
            });
            //$(closeSelect).html('myArray');
            $(closeSelect).replaceOptions(myArray);

            //Set selectedValue over here
            $(closeSelect).val(yourSelectedValue);
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM