【问题标题】:How to let the current type status of the customer show in the drop-down menu如何让客户的当前类型状态显示在下拉菜单中
【发布时间】:2015-07-08 03:37:46
【问题描述】:

这里是选择框/下拉菜单:

 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>';

模态对话框:

 bootbox.dialog({
            onEscape:true,
            backdrop:true,
          message: '<div class="row">  ' +
                   '<div class="col-md-12"> ' +
                   '<form class="form-horizontal"> ' +
                   '<div class="form-group"> ' +
                   '<label class="col-md-4 control-label" for="awesomeness">Phone: </label> ' +
                   '<div class="col-md-4">' +
                   '<input id="edit-phone_no" type="text" value="'+table_column_7+'"/>' +
                   '</div><br>' +
                   '<label class="col-md-4 control-label" for="awesomeness">Name: </label> ' +
                   '<div class="col-md-4">' +
                   '<input id="edit-name" type="text" value="'+table_column_2+'"/>' +
                   '</div><br>' +
                   '<label class="col-md-4 control-label" for="awesomeness">Type: </label> ' +
                   '<div class="col-md-4">' +type_select+
'</div>'+
                   '</form> </div>  </div>'});

输入电话号码时自动显示客户姓名和类型的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);

                    }
                 }
             });
     }
}

PHP 函数根据电话号码从数据库中获取客户的姓名和类型(组名):

public function get_name_by_phone_no($phone_no){
        $result = "";
        $this->db->select('name,group_id');
        $this->db->where('phone_no',$phone_no);
        $query = $this->db->get('customer');
        if ($query->num_rows() > 0){
        $row = $query->row();
        $group_id = $row->group_id;
        $row->group_name = $this->get_group_name_by_group_id($group_id);
        $result = $row;
        }
        echo json_encode($result);

    }

当我输入电话号码时。客户,我希望名称自动显示在文本框中,并在下拉菜单中自动选择客户类型(全部基于数据库中的记录)。名称部分现在可以使用,但类型部分不起作用。一定有问题。请告诉我如何解决它。非常感谢大家提前提供的帮助。

【问题讨论】:

  • 任何人请帮助我-谢谢

标签: javascript php database web modal-dialog


【解决方案1】:

你的逻辑错误。 当您创建 SELECT 框(下拉菜单)时,您将选项值设置为 group_id,而另一侧则返回组名:

   $row->group_name = $this->get_group_name_by_group_id($group_id)

ajax 调用中来自服务器的组名。

所以你的错误是Ajax响应有组名称在id中的id oc so

  $('#type_select').val(data.group_name);

data.group_name 与选项中的任何值都不匹配,因此它不起作用。

【讨论】:

    【解决方案2】:

    更改 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);//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');
    
                        }
                     }
                 });
         }
    }
    

    现在可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2020-12-09
      • 2018-08-16
      相关资源
      最近更新 更多