选择列表首先通过别人不通过

时间:2014-11-19 10:33:11

标签: php jquery

www.mpoo.org/organizatori/drugi.php中的

有2个选择列表,第一次工作但其他没有。这与this非常相似。

Debugger第二次说:

  

TypeError:$(...)为空

     

drugi.php:37

     

var country_id = $(“select#drop1 option:selected”)。attr('value');

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Списак организатора</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="style.css" type="text/css" />
<style type="text/css">
<!--
@import url("stil.css"); 
-->
</style>

</head>
<body>
  <div id="body">
    <div class="mhead"><h2>Списак организатора</h2></div>
    <div id="dropdowns">
       <div id="center" class="cascade">
                <label>Одабери претрагу:
                <select  name="country" id = "drop1">
                    <option value=""> Одабери...</option>
                    <option value="Grad"> Град/Општина</option>
                    <option value="Zanimanje"> Занимање</option>
                    <option value="Struka"> Струка</option>
                    <option value="Organizator"> Организатор</option>
                    <option value="Svi"> Сви организатори</option>
                </select>
                </label>
          </div>
        <div class="cascade" id="state"></div> 
      </div>
      <div id="city"> </div> 
  </div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("select#drop1").change(function(){

    var country_id =  $("select#drop1 option:selected").attr('value'); 
// alert(country_id);   
    $("#state").html( "" );
    $("#city").html( "" );
    if (country_id.length > 0 ) { 
        if (country_id=='Svi'){
        $.ajax({
                type: "POST",
                url: "drugi4.php",
                data: "country_id="+country_id,
                cache: false,
                beforeSend: function () { 
                    $('#state').html('<img src="loader.gif" alt="" width="24" height="24">');
                },
                success: function(html) {    
                    $("#state").html( html );
                }
            });
        }

        else {  
        $.ajax({
                type: "POST",
                url: "drugi2.php",
                data: "country_id="+country_id,
                cache: false,
                beforeSend: function () { 
                    $('#state').html('<img src="loader.gif" alt="" width="24" height="24">');
                },
                success: function(html) {    
                    $("#state").html( html );
                }
            });
        }
    }
});
});
</script>
</body>
</html>

这是drugi2.php

    <?php
include("connection.php");
//var_dump($_POST); 
$state_id = trim(mysqli_escape_string($con, $_POST["country_id"]));
$sql="SELECT DISTINCT $state_id FROM jom_x1_organizatori ORDER BY $state_id";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
    mysqli_set_charset($con, "utf8");
    $query = mysqli_query($con, $sql);
    ?>
    <label>
        <select name="city" id = "drop2">
            <option value="">Одабери...</option>
            <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
            <option value="<?php echo $rs[$state_id]; ?>"><?php echo $rs[$state_id]; ?></option>
        <?php } ?>
        </select>
    </label>
<?php 
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("select#drop2").change(function(){

    var state_id = $("select#drop2 option:selected").attr('value')+";"+$("select#drop1 option:selected").attr('value');
//    alert(state_id);
    if (state_id.length > 0 ) { 
     $.ajax({
            type: "POST",
            url: "drugi3.php",
            data: "state_id="+state_id,
            cache: false,
            beforeSend: function () { 
                $('#city').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#city").html( html );
            }
        });
    } else {
        $("#city").html( "" );
    }
});

});
</script>

1 个答案:

答案 0 :(得分:0)

我认为在ajax加载后删除所选的选项,因此你应该使用:

var country_id = $("#drop1").val();

而不是

var country_id = $("select#drop1 option:selected").attr('value');