简体   繁体   English

如何选择基于其值添加到客户端的选择选项

[英]How to select a select option added on the client based on its value

I am unable to select from options that are added dynamically by jquery. 我无法从jquery动态添加的选项中进行选择。 This is where l am adding the options: 我在这里添加选项:

$.get($('#baseurl').val() + '/category-products', {id : category_id}, function(response){
        if(response.success)
        {
            var product_select = $('#product_select').empty();
            $.each(response.products, function(i, product){
                $('<option/>', {
                    value: product.id.toString(),
                    text: product.name
                }).appendTo(product_select);
            });
            set_product();
        }
    });

This is where l am trying to select an option manually: 我在这里尝试手动选择一个选项:

$('#product_select').val($('#pres_product').val()).change();

The pres_product is an hidden tag with the value of the option i wish to select. pres_product是一个隐藏的标签,带有我希望选择的选项的值。 I have checked the value of $('#pres_product').val() to ensure its correct. 我检查了$('#pres_product').val()以确保其正确。 I have noticed that selection on a select with options that were added from the server works perfectly. 我注意到,从服务器添加的选项中的select上的选择效果很好。

您应该设置selected属性

$('#product_select').val($('#pres_product').val()).prop('selected', true);

 var newData = [{ id: 4, name: 'four' }, { id: 5, name: 'five' }, { id: 6, name: 'six' }]; var hiddenInput = $('#pres_product'); var product_select = $('#product_select').change(function() { console.log(this.value); }); function changeOptions() { product_select.empty(); var html = ""; newData.forEach(function(d) { html += '<option value="' + d.id + '">' + d.name + '</option>' }) product_select.html(html); } function setProduct() { val = hiddenInput.val(); //can be string also "5" product_select.val(val).change(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="hidden" value="5" id="pres_product" /> <select id="product_select"> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select> <button onclick="changeOptions();"> Change Select </button> <button onclick="setProduct()"> Set New Value </button> 

I solved it by causing a delay before selection of the item. 我通过在选择项目之前造成延迟来解决了该问题。 It seems that it was not fully loaded if the selection of the value was done immediately. 如果立即选择值,则似乎未完全加载。 Below is the code: 下面是代码:

 $.blockUI({ css: { backgroundColor: '#337AB7', color: '#fff'}, baseZ: 2000 });
    $.get($('#baseurl').val() + '/category-products', {id : category_id}, function(response){
        if(response.success)
        {
            var product_select = $('#product_select').empty();
            $.each(response.products, function(i, product){
                $('<option/>', {
                    value: product.id.toString(),
                    text: product.name,
                }).appendTo(product_select);
            });
        }
    });
    var delay = 1000;
    //allow for the items to have loaded properly
    setTimeout(function() {
        $('#product_select').val($('#pres_product').val()).change();
        $.unblockUI();
    }, delay);

I noticed that accidentally when i used an alert just before trying to select. 我在尝试选择之前使用警报时意外地注意到了这一点。 It worked well so l figured out that the items needed a little bit of time to load properly. 效果很好,所以我发现这些项目需要一点时间才能正确加载。

暂无
暂无

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

相关问题 如何禁用 <option>在一个<select>基于它在 JavaScript 中的值?我有一个带有多个 &lt;option&gt; 的 &lt;select&gt; 。每一个都有独特的价值。我需要禁用具有给定定义值(而不是 innerHTML )的 &lt;option&gt; 。有人知道怎么做吗?</select></option> - How can I disable an <option> in a <select> based on its value in JavaScript? 如何选择标签 <option>给定它的值,并给它起一个名字<select> - How to select the label of an <option> given its value, and given a name for the <select> 使用jQuery根据其文本而非值从菜单中选择选项 - use jQuery to select option from menu based on its text not value 如何根据孩子的价值选择父母? - how to select parent based on value of its child? 如何根据其动态值选择列表 - How to select the list based on its dynamic value 如何制作选择下拉列表以根据当前时间选择选项值 - How make a select dropdown to select an option value based on current time 如何根据 javascript 的数量计算 select 选件的价格 - how to count the price of select option based on its quantity with javascript Puppeteer:如何根据文本选择下拉选项? - Puppeteer: How to select a dropdown option based on its text? 如何根据所选选项的值更改选择的选项? - How to change select's option based on a selected option's value? 根据其他 select 值隐藏 select 中的选项 - Hide option in select based on other select value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM