Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Javascript-Ajax转换为jQuery-Ajax的最佳方法_Javascript_Jquery_Ajax - Fatal编程技术网

将Javascript-Ajax转换为jQuery-Ajax的最佳方法

将Javascript-Ajax转换为jQuery-Ajax的最佳方法,javascript,jquery,ajax,Javascript,Jquery,Ajax,将此代码转换为jquery的最佳方式是什么?我试过这样做,但注意到internet explorer给我带来了麻烦。所以我决定用jQuery实现它,但我看到了很多方法,但我不知道如何用它实现相同的函数 function updateField(str, id, prevvalue, value, vehicletype) { if (str == "") { document.getElementById(id).innerHTML = ""; retur

将此代码转换为jquery的最佳方式是什么?我试过这样做,但注意到internet explorer给我带来了麻烦。所以我决定用jQuery实现它,但我看到了很多方法,但我不知道如何用它实现相同的函数

function updateField(str, id, prevvalue, value, vehicletype) {
    if (str == "") {
        document.getElementById(id).innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(id).innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "inc/form_rest.php?q=" + str + "&prevvalue=" + prevvalue + "&value=" + value + "&vehicletype=" + vehicletype, true);
    xmlhttp.send();
}

在使用jQuery时,您不必担心浏览器。只需使用jQuery Ajax
方法。
使用


最简单的方法是使用:

请注意,我让jQuery构建url,这也将确保正确完成url编码

因为您只使用响应来填充元素,所以也可以使用

$('#'+id).load(
   "inc/form_rest.php?q=" + str + "&prevvalue=" + prevvalue + "&value=" + value + "&vehicletype=" + vehicletype
);

您正在执行GET请求,因此使用以下选项是有意义的:


非常简单,只需调用一个方法:

function updateField(str, id, prevvalue, value, vehicletype) {
  if (str=="")
  {
    $('#' + id).html("");
    return;
  } 
  $.get("inc/form_rest.php", 
        {"prevvalue": prevvalue, "value":value /*and so on*/},
        function(data) {
          document.getElementById(id).innerHTML = data;  //this is compatible, no jQuery needed!
        }
  );

}
试一试

还是简单

$.get('inc/form_rest.php?q='+str+'&prevvalue='+prevvalue+'&value='+value+'&vehicletype='+vehicletype, function(data) {
    document.getElementById(id).innerHTML=data;
});
请执行以下操作:

  • 删除检查浏览器然后创建xmlhttp对象的代码
  • 您不需要任何显式回调方法,如xmlhttp.onreadystatechange=function
  • 直接调用jQueryajax方法

  • 我认为这应该奏效:

    function updateField(str, id, prevvalue, value, vehicletype){
          var request = $.ajax({
              url: "inc/form_rest.php",
              type: "GET",
              data: { "str" : str, 
                      "prevvalue" : prevvalue, 
                      "value" : value, 
                      "vehicletype" : vehicletype
              },
              'success': function(){
                 //successful request
              }
        });
    
    你的例子是:

    var url = "inc/form_rest.php?q="+str+"&prevvalue="+prevvalue+"&value="+value+"&vehicletype="+vehicletype;
    
    $.get(url,function(data){
     $("#"+id).html(data); 
     });
    

    最好的方法是学习jQuery。简单的方法是遵循回答你问题时给出的好建议。我理解,但我必须选择简单的方法,因为我处于hurry@JanDvorak:即将更新我的答案。让我知道我是否应该添加更多的东西谢谢你,但是我如何做才能像javascript那样给函数命名和参数,这样我就可以调用it@SinanSamet:我已经更新了我的答案。只需访问jQuery文档链接,您就可以获得其余内容。@Downvoter:我可以知道奖励背后的原因吗?
    prevvalue
    应该在q参数上加引号吗?@TomSarduy不,您不需要在引号之间加属性名。最后一个函数做什么?函数(html){$('#'+id).html(html)}如果是这样的话,我会在脚本中的其他地方调用该函数。它与
    document.getElementById(id).innerHTML=html
    ,以一种更具jquery风格的方式。非常感谢这两种方式的工作!我选择了第一个,因为它看起来更安全。
    $.ajax({
        url: 'inc/form_rest.php?q='+str+'&prevvalue='+prevvalue+'&value='+value+'&vehicletype='+vehicletype,
        method: 'GET',
        success: function(data, textStatus, jqXHR) {
            document.getElementById(id).innerHTML=data;
        }
    
    });
    
    $.get('inc/form_rest.php?q='+str+'&prevvalue='+prevvalue+'&value='+value+'&vehicletype='+vehicletype, function(data) {
        document.getElementById(id).innerHTML=data;
    });
    
    function updateField(str, id, prevvalue, value, vehicletype){
          var request = $.ajax({
              url: "inc/form_rest.php",
              type: "GET",
              data: { "str" : str, 
                      "prevvalue" : prevvalue, 
                      "value" : value, 
                      "vehicletype" : vehicletype
              },
              'success': function(){
                 //successful request
              }
        });
    
    var url = "inc/form_rest.php?q="+str+"&prevvalue="+prevvalue+"&value="+value+"&vehicletype="+vehicletype;
    
    $.get(url,function(data){
     $("#"+id).html(data); 
     });