Javascript 如何在下拉菜单中显示客户的当前类型状态
这是选择框/下拉菜单:Javascript 如何在下拉菜单中显示客户的当前类型状态,javascript,php,database,web,modal-dialog,Javascript,Php,Database,Web,Modal Dialog,这是选择框/下拉菜单: var type_select = '<select id="type_select" style="margin-bottom:0px;">'; var i; var customer_group = <?php echo json_encode($customer_group);?>; for (i = 0; i < customer_group.length; ++i) {
var type_select = '<select id="type_select" style="margin-bottom:0px;">';
var i;
var customer_group = <?php echo json_encode($customer_group);?>;
for (i = 0; i < customer_group.length; ++i) {
//console.log(customer_group[i].group_id);
if (customer_group[i].group_name == table_column_3){
type_select = type_select+'<option value='+customer_group[i].group_id+' selected="selected">'+customer_group[i].group_name+'</option>';
}else{
type_select = type_select+'<option value='+customer_group[i].group_id+'>'+customer_group[i].group_name+'</option>';
}
}
type_select = type_select+'</select>';
当我输入客户的电话号码时,我希望名称自动显示在文本框中,并根据数据库中的记录在下拉菜单中自动选择客户类型。名称部分现在可以工作,但类型部分不工作。一定有问题。请告诉我怎么修。非常感谢大家事先提供的帮助。您的逻辑错误。
创建“选择框”下拉菜单时,您将选项值设置为组id,另一侧返回组名称:
$row->group_name = $this->get_group_name_by_group_id($group_id)
ajax调用中来自服务器的组名
所以,您的错误是,当选项值位于id So中时,ajax响应具有组名
$('#type_select').val(data.group_name);
data.group\u name与选项中的任何值都不匹配,因此它不起作用。更改Javascript/AJAX函数,以便在输入电话号码时自动显示客户的姓名和类型
document.getElementById('edit-phone_no').onkeyup = function(){
text_length = $('#edit-phone_no').val().length;
if (text_length >= 8){
$.ajax({
url : "<?php echo base_url(); ?>index.php/home/get_name_by_phone_no",
type: "post",
data: {
"phone_no" : $('#edit-phone_no').val(),
},
success: function(response){
console.log(response);
var data = JSON.parse(response);
if (response != ""){
$('#edit-name').val(data.name);
$('#type_select').val(data.group_name);
}
}
});
}
}
原件:
document.getElementById('edit-phone_no').onkeyup = function(){
text_length = $('#edit-phone_no').val().length;
if (text_length >= 8){
$.ajax({
url : "<?php echo base_url(); ?>index.php/home/get_name_by_phone_no",
type: "post",
data: {
"phone_no" : $('#edit-phone_no').val(),
},
success: function(response){
console.log(response);
var data = JSON.parse(response);
if (response != ""){
$('#edit-name').val(data.name);
$('#type_select').val(data.group_name);//original
}
}
});
}
}
现在:
现在可以用了。任何人都可以帮我-谢谢
$('#type_select').val(data.group_name);
document.getElementById('edit-phone_no').onkeyup = function(){
text_length = $('#edit-phone_no').val().length;
if (text_length >= 8){
$.ajax({
url : "<?php echo base_url(); ?>index.php/home/get_name_by_phone_no",
type: "post",
data: {
"phone_no" : $('#edit-phone_no').val(),
},
success: function(response){
console.log(response);
var data = JSON.parse(response);
if (response != ""){
$('#edit-name').val(data.name);
$('#type_select').val(data.group_name);//original
}
}
});
}
}
document.getElementById('edit-phone_no').onkeyup = function(){
text_length = $('#edit-phone_no').val().length;
if (text_length >= 8){
$.ajax({
url : "<?php echo base_url(); ?>index.php/home/get_name_by_phone_no",
type: "post",
data: {
"phone_no" : $('#edit-phone_no').val(),
},
success: function(response){
console.log(response);
var data = JSON.parse(response);
if (response != ""){
$('#edit-name').val(data.name);
/*replaced code*/
var store;
var types = ["N/A","VIP","Writer","High","Low","No Show","Black List","Cancel","Family","Friend"];
for(i=0;i<types.length;i++)
{
if(types[i]==data.group_name)
store=i;
}
$("#type_select option:contains(" + types[store] + ")").attr('selected', 'selected');
}
}
});
}
}