weixin_33739646 2016-02-05 15:27 采纳率: 0%
浏览 28

使用ajax刷新div

I need to refresh a select (by id) when the ajax call is successful. But i have no idea how to process

ajax function :

$('#insertForm').on('click', function(){
  var form_intitule = $('input[name=form_intitule]').val();
  $.ajax({
    type: "GET",
    url: "lib/function.php?insertForm="+insertForm+"&form_intitule="+form_intitule,
    dataType : "html",
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      alert(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
    },
    success:function(data){
      /*here i need to reload #listeFormation*/      }
  });
});

html.php

<div class="form-group">
  <label for="nomSalarie" class="col-sm-1 control-label" id="nameSelect">Formations</label>
  <div class="col-sm-11">
    <select name="listeFormation[]" id="listeFormation" class="form-control" multiple>';
      while($ligne = $displayFormation->fetch()){
        $data['formation'] .='<option value="'. $ligne['form_id'].'">'.$ligne['form_intitule']. " [" . $ligne['form_organisme'] . "]".'</option>';
      }
    </select>
  </div>
</div>
  • 写回答

1条回答 默认 最新

  • weixin_33737134 2016-02-05 16:02
    关注

    There are 2 ways in my thought:

    1. Use the $.load() from jQuery like: (it's a GET which inject the response into the node you select)

    $('#insertForm').on('click', function(){
      var form_intitule = $('input[name=form_intitule]').val();
      
      $("#listeFormation").load( 
        "lib/function.php", 
        { 
          insertForm: insertForm,
          form_intitule: form_intitule
        },
        function(response, status, xhr){
          if(status == "error"){
            alert(xhr + '--' + xhr.status + " " + xhr.statusText);
          }else{
            // Process the HTML you want here.. lets call: 'html_you_want_to_inject'
            
            $('#listeFormation').html(html_you_want_to_inject);  
          }
        }
      });
    });

    1. Using your code, you could use simplely:

    $("#listeFormation").html(html_you_want_to_inject);

    I guess one point to think is about to remove the PHP from the div and turn this div dinamic since the page load with your Javascript. You kill your PHP each time the AJAX runs, rewriting the content in runtime.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器