[英]When editing a form in a modal, how do I get the previously selected option from a dependent dropdown?
我有两张桌子,一张给顾客,一张给他们的车。 提交表格时,车辆取决于您的客户 select。 现在当我 go 编辑表格时,我可以像这样更改客户 select 车辆...
$(document).on('change','#customeredit', function(){
var customer_id = $(this).val();
if(customer_id){
$.ajax({
type:'POST',
url:'get-vehicle-from-customer.php',
data:{'customer_id':customer_id},
success:function(result){
$('#vehicleedit').html(result);
}
});
}else{
$('#vehicleedit').html('<option value="">Vehicle</option>');
}
});
但我一生无法弄清楚如何让它自动显示车辆列表而不首先更改客户选择。 我知道我在这里遗漏了一些简单的东西,但我对此并不陌生,并且真的可以使用一些方向。 谢谢!
我会将你的 ajax 调用定义为 function,然后你可以在任何你想要的地方调用它:
var fx_load_customer = function()
{
//var customer_id = $(this).val();
var customer_id = $('#customeredit').val();
if(customer_id){
$.ajax({
type:'POST',
url:'get-vehicle-from-customer.php',
data:{'customer_id':customer_id},
success:function(result){
$('#vehicleedit').html(result);
}
});
}else{
$('#vehicleedit').html('<option value="">Vehicle</option>');
}
}
$(document).on('change','#customeredit', function(){ fx_load_customer(); });
fx_load_customer();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.