Php Jquery-无法自动选择从回调派生的特定选项
目标:页面加载时默认选择产品三,使用Jquery,而不是硬编码 来自MySQL的数据: HTMLPhp Jquery-无法自动选择从回调派生的特定选项,php,jquery,Php,Jquery,目标:页面加载时默认选择产品三,使用Jquery,而不是硬编码 来自MySQL的数据: HTML 如果能提供任何解决方案,我们将不胜感激。谢谢。以下两种变体都适合我: $('#product-selection option[value="pd3"]').prop("selected", true) $('#product-selection option[value="pd3"]').prop("selected", "selected") 确保在插入服务器的HTML后运行此代码: $(do
如果能提供任何解决方案,我们将不胜感激。谢谢。以下两种变体都适合我:
$('#product-selection option[value="pd3"]').prop("selected", true)
$('#product-selection option[value="pd3"]').prop("selected", "selected")
确保在插入服务器的HTML后运行此代码:
$(document).ready(function() {
$.post('load.php', {
req: 'load_product'
}, function(data, status) {
$("#product-selection").empty().append(data);
// Selection code needs to go into this callback,
// which is executed after the response comes back.
$('#product-selection option[value="pd3"]').prop("selected", "selected")
});
// Selection code SHOULD NOT be here, because this is executed
// right after the POST request is made.
// When code at this place is executed, the response has most
// likely not come back yet.
});
请检查一下。
您可以设置动态值“pdvalue”。当页面加载时,您可以使用它
var pdvalue=pd2;
$document.readyfunction{
$.post'load.php'{
请求:“加载产品”
},功能数据,状态{
$product-selection.empty.APPENDATA;
};
//$'product-selection'.valpd3;谢谢。对于我的情况,Jquery回调加载在一个模板中,当用户访问HTML页面时,它首先调用模板,然后选择与用户相关的相应产品。因此,我无法将选择代码放入回调模板中,即在functiondata、status之后。
$(document).ready(function() {
$.post('load.php', {
req: 'load_product'
}, function(data, status) {
$("#product-selection").empty().append(data);
});
// $('#product-selection').val("pd3"); <-Refer to Item 5. Problem
});
Callback Return:
<option selected disabled></option>
<option value="pd1">Product One</option>
<option value="pd2">Product Two</option>
<option value="pd3">Product Three</option>
<option value="pd4">Product Four</option>
1. $('#product-selection').val("pd3");
2. $('#product-selection').val("pd3").change();
3. $('#product-selection').val("pd3").trigger('change');
4. $('#product-selection option[value="pd3"]').prop('selected', true);
5. $('#product-selection option[value="pd3"]').prop('selected', true).change();
6. $('#product-selection option[value="pd3"]').prop('selected', true).trigger('change');
Have tried prop / attr, ('selected', true) / ('selected', 'selected') as well. Not working.
$('#product-selection option[value="pd3"]').prop("selected", true)
$('#product-selection option[value="pd3"]').prop("selected", "selected")
$(document).ready(function() {
$.post('load.php', {
req: 'load_product'
}, function(data, status) {
$("#product-selection").empty().append(data);
// Selection code needs to go into this callback,
// which is executed after the response comes back.
$('#product-selection option[value="pd3"]').prop("selected", "selected")
});
// Selection code SHOULD NOT be here, because this is executed
// right after the POST request is made.
// When code at this place is executed, the response has most
// likely not come back yet.
});