[英]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.