jQuery代码导致结果冲突

时间:2011-07-23 10:20:09

标签: php javascript jquery

我想问一下,有没有更好的方法来运行此代码。我有一个选择#BA_customer的表单,当选择它时会填充第二个菜单,其中包含所选客户端的地址。我还需要根据客户选择显示部门下拉列表。我认为我的代码导致了一个冲突,即客户在2个单独的语句中被传递了两次。这样做的正确方法是什么?非常感谢

    <script language="javascript" type="text/javascript">
          $(function() {
                $("#BA_customer").live('change', function() { if ($(this).val()!="")
                $.get("../../getOptions.php?BA_customer=" + $(this).val(), function(data) {
                $("#BA_address").html(data); });
            }); 
          });
    </script>
    <script language="javascript" type="text/javascript">
          $(function() {
                $("#BA_customer").live('change', function() { if ($(this).val()!="")
                $.get("../../getDept.php?BA_customer=" + $(this).val(), function(data) {
                $("#BA_dept").html(data); });
            }); 
          });
    </script>

+++++++ dept.php Code ++++++++++++++++++++++++++++++++++++++

$customer = mysql_real_escape_string( $_GET["BA_customer"] ); // not used here, it's the customer choosen

    $con = mysql_connect("localhost","root","");
    $db = "sample";
      if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($db, $con);
        $query_rs_select_dept = sprintf("SELECT * FROM departments where code = '$customer'");
        $rs_select_dept = mysql_query($query_rs_select_dept, $con) or die(mysql_error());
        $row_rs_select_dept = mysql_fetch_assoc($rs_select_dept);
        $totalRows_rs_select_dept = mysql_num_rows($rs_select_dept);



          echo '<label for="dept">Select a Department</label>'.'<select name="customerdept">';
          echo '<option value="">Select a Department</option>';
          while ($row_rs_select_dept = mysql_fetch_assoc($rs_select_dept))
          {
          $dept=$row_rs_select_dept['name'];
          echo '<option value="dept">'.$dept.'</option>';
          }
          echo '</select>';

++++ SOLUTION +++++++++++++++++++++++++++++

mysql_select_db($db, $con);
$query_rs_dept = sprintf("SELECT * FROM departments where code = '$customer'");
$rs_dept = mysql_query($query_rs_dept, $con) or die(mysql_error());
$totalRows_rs_dept = mysql_num_rows($rs_dept);

echo '<label for="dept">Select a Department</label>'.'<select name="customerdept">';
echo '<option value="">Select a Department</option>';
while ($row_rs_dept = mysql_fetch_assoc($rs_dept))
{
       $dept=$row_rs_dept['name'];
       echo '<option value="dept">'.$dept.'</option>';
}
echo '</select>';

删除第一个$ row_rs_select_dept = mysql_fetch_assoc($ rs_select_dept);从脚本,它的工作原理。刚刚发布解决方案以防其他灵魂需要帮助解决这样的问题。

1 个答案:

答案 0 :(得分:1)

也许你可以一起发送这两个请求?

$(function() {
    $("#BA_customer").live('change', function() { 
        if($(this).val()!="")
            $.get("../../getOptions.php?BA_customer=" + $(this).val(), function(data) {
                $("#BA_address").html(data); 
            });
            $.get("../../getDept.php?BA_customer=" + $(this).val(), function(data) {
                $("#BA_dept").html(data); 
            });
        }); 
});