Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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
如何改进jQuery代码?_Jquery_Mysql - Fatal编程技术网

如何改进jQuery代码?

如何改进jQuery代码?,jquery,mysql,Jquery,Mysql,我对使用jQuery比较陌生。我编写了以下代码,该代码与一个表单一起工作,该表单填充了一年的三个框、一辆车的品牌和型号。年份是用php编码的,当用户单击年份框时已经列出。make和model从mysql数据库中调用,以填充列表供其选择 但是到目前为止,对make和model的查询在填充列表之前有一点滞后。这里有什么可以改进代码的地方吗?提前感谢您的帮助 $(function(){ $('#year').change(function getmakes(){ var vehyear = $

我对使用jQuery比较陌生。我编写了以下代码,该代码与一个表单一起工作,该表单填充了一年的三个框、一辆车的品牌和型号。年份是用php编码的,当用户单击年份框时已经列出。make和model从mysql数据库中调用,以填充列表供其选择

但是到目前为止,对make和model的查询在填充列表之前有一点滞后。这里有什么可以改进代码的地方吗?提前感谢您的帮助

$(function(){
$('#year').change(function getmakes(){
    var vehyear = $("#year").val();
    $("#model").html('<option value="">-- All Models --</option>');
    $.ajax({
           url: "/widgets/makes.php",
           global: false,
           type: "POST",
           async: false,
           dataType: "html",
           data: "year="+vehyear, //the name of the $_POST variable and its value
           success: function (response) //'response' is the output provided
                       {
                    //counts the number of dynamically generated options
                    var dynamic_options = $("*").index( $('.dynamic')[0] );
                    //removes previously dynamically generated options if they exists (not equal to 0)
                    if (dynamic_options != (-1)) $(".dynamic").remove();
                    $("#make").html(response);
                    $(".first").attr({selected: ' selected'});

                   }
          });
          return false
});
$('#make').change(function getmodels(){
    var vehyear = $("#year").val();
    var vehmake = $("#make").val();
    $.ajax({
           url: "/widgets/models.php",
           global: false,
           type: "POST",
           async: false,
           dataType: "html",
           data: "year="+ vehyear+"&make="+vehmake, //the name of the $_POST variable and its value
           success: function (response) //'response' is the output provided
                       {
                     //counts the number of dynamically generated options
                    var dynamic_options = $("*").index( $('.dynamic')[0] );
                    //removes previously dynamically generated options if they exists (not equal to 0)
                    if (dynamic_options != (-1)) $(".dynamic").remove();
                    $("#model").html(response);
                    $(".first").attr({selected: ' selected'});                        
                   }
          });
          return false
});              

})