2

将此代码转换为 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();
}
4

8 回答 8

5

最简单的方法是使用get

$.get("inc/form_rest.php",
    {q:str, prevvalue:prevvalue, value:value, vehicletype:vehicletype},
    function(html){
       $('#'+id).html(html);
    }
);

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

由于您仅使用响应来填充元素,因此您也可以使用

$('#'+id).load(
   "inc/form_rest.php?q=" + str + "&prevvalue=" + prevvalue + "&value=" + value + "&vehicletype=" + vehicletype
);
于 2013-02-04T12:02:49.233 回答
3

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

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!
        }
  );

}
于 2013-02-04T12:03:11.607 回答
2

你正在做一个 GET 请求,所以使用它是有意义的jQuery.get

$.get("inc/form_rest.php", {
    q : str,
    prevvalue : prevvalue,
    value : value,
    vehicletype : vehicletype
}, function(response) {
    $('#' + id).html(response);
});
于 2013-02-04T12:03:09.993 回答
0

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

$.get('inc/form_rest.php',  // URL
      {q:str, prevvalue:prevvalue, value:value,vehicletype:vehicletype}, // Data
      function(data) {
      // Do post Ajax things here
});
于 2013-02-04T12:00:20.757 回答
0

尝试

$.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;
});
于 2013-02-04T12:03:13.340 回答
0

执行以下操作:

  1. 删除检查浏览器然后创建xmlhttp 对象的代码。
  2. 您不需要像 xmlhttp.onreadystatechange = function这样的显式回调方法。
  3. 直接把你的jQuery ajax 方法调用
于 2013-02-04T12:04:19.843 回答
0

我认为这应该有效:

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
          }
    });
于 2013-02-04T12:06:08.000 回答
0

那么你的例子:

var url = "inc/form_rest.php?q="+str+"&prevvalue="+prevvalue+"&value="+value+"&vehicletype="+vehicletype;

$.get(url,function(data){
 $("#"+id).html(data); 
 });
于 2013-02-04T12:06:28.913 回答