检测两个选择字段的.change - Jquery

时间:2011-08-23 21:19:55

标签: php jquery ajax forms

我不是jquery专家,所以我需要一些关于现有代码的帮助。

当#drop2 AND #drop2_1已更改时,我希望我的jquery ajax功能正常工作。 此时此功能仅在#drop2 OR #drop2_1更改时有效。 :(

Jquery的:

$('#wait_2').hide();
$('#drop_2 && #drop_2-1').change(function(){
  $('#wait_2').show();
  $('#result_2').hide();
  $.get(\"func.php\", {
    func: \"drop_2\",
    drop_var: $('#drop_2').val()
  }, function(response){
    $('#result_2').fadeOut();
    setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
  });
    return false;
});

PHP代码:

if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
   drop_1($_GET['drop_var']);
}

function drop_1($drop_var)
{
    include_once('db.php');
    $result = mysql_query("SELECT DISTINCT tier_two FROM Producten_verkoop WHERE tier_one='$drop_var'");
    $result2 = mysql_query("SELECT DISTINCT tier_three FROM Producten_verkoop WHERE tier_one='$drop_var'")

    or die(mysql_error());

    echo '<select name="drop_2" id="drop_2">
          <option value=" " disabled="disabled" selected="selected">Choose one</option>';

           while($drop_2 = mysql_fetch_array( $result ))
            {
              echo '<option value="'.$drop_2['tier_two'].'">'.$drop_2['tier_two'].'</option>';
            }

    echo '</select>';


            echo '<select name="drop_2-1" id="drop_2-1">
          <option value=" " disabled="disabled" selected="selected">Choose one</option>';

           while($drop_2 = mysql_fetch_array( $result2 ))
            {
              echo '<option value="'.$drop_2['tier_three'].'">'.$drop_2['tier_three'].'</option>';
            }

    echo '</select>';

其他解决方案,例如检测非空值也是受欢迎的。

1 个答案:

答案 0 :(得分:2)

您提供的选择器不起作用。您必须编写自定义逻辑来实现逻辑。试试这个

function getFunc(){

  //Reset the dropdownChanged values
  dropDown2_1Changed = false;
  dropDown2Changed = false;

  $('#wait_2').show();
  $('#result_2').hide();
  $.get("func.php", {
      func: "drop_2",
      drop_var: $('#drop_2').val()
    }, 
    function(response){
      $('#result_2').fadeOut();
      setTimeout(function(){
        finishAjax_tier_three('result_2', escape(response));
      }, 400);
  });
}

var dropDown2Changed = false; dropDown2_1Changed = false;

$('#drop_2').change(function(){
   dropDown2Changed = true;
   if(dropDown2_1Changed){
      getFunc();
   }
});

$('#drop_2-1').change(function(){
   dropDown2_1Changed = true;
   if(dropDown2Changed){
      getFunc();
   }
});