【问题标题】:data not show ajax and javascript ci数据不显示 ajax 和 javascript ci
【发布时间】:2020-10-13 16:19:05
【问题描述】:

我想使用 ajax , javascript snd CI php select option cat_id click after show relataed sub-cat-id but not show data 。我正在测试警报数据显示正常但无法获取 html 数据请在此处分享有价值的想法先生分享代码..

ajax javascript

<script>
     $(document).ready(function() {
    $('#catid').on('click', function() {
           var catid = $("#catid").val();

           $.ajax({
               url: "<?php echo base_url(); ?>get-form-detail",
               type: "post",
               data: "catid=" + catid,
                success: function(response) {
                   var res = JSON.parse(response);
                   
                  for(i=0;i<res.length;i++)
                            {
                                
                                //alert(res[i]['cat_title']);
                                
                                options +=  '<option value="' + res[i]['id'] + '">' + res[i]['cat_title'] + '</option>';
                                $('#tbl').html(options); 
                            }
                    
                 
               }
           });
       });
   });  
   </script>

查看页面

<div class="row">
                        <label class="col-sm-4 col-form-label text-right">Form Category : </label>
                        <div class="col-sm-8">
                           <div class="form-group">
                              <select class="form-control" name="formID" id="formID">
                                <div id="tbl"></div> 
                              </select>
                           </div>
                        </div>
                     </div>

【问题讨论】:

    标签: javascript ajax


    【解决方案1】:

    将回调函数改为

    success: function(response) {
        let res = JSON.parse(response);
        let options = res.reduce((a,v)=>(`${a}<option value = "${v['id']}">${v['cat_title']}</option>`), '');
        $('#formID').html(options);                 
    }
    

    并使用此 html 代码

    <select class="form-control" name="formID" id="formID"></select>
    

    【讨论】:

      【解决方案2】:

      知道你如何使用变量选项添加更多项目,但我有另一个解决方案

      替换您的代码:

         var x = document.getElementById("yourSelectElementId");
      
         for(i=0;i<res.length;i++){
                                      
              var option = document.createElement("option");
      
              option.text = res[i]['id'];
              option.value= res[i]['id'];
      
              x.add(option);
                                    
          }
      

      【讨论】:

        猜你喜欢
        • 2016-05-19
        • 1970-01-01
        • 1970-01-01
        • 2020-05-12
        • 1970-01-01
        • 1970-01-01
        • 2013-10-01
        • 2018-03-02
        • 1970-01-01
        相关资源
        最近更新 更多