0

我希望从下拉框中获取选定的字段,以便在将来的下拉框中使用它,但我不知道如何通过 ajax 将变量回显到 html。

<p>Trainer</p>
<select name = "trainer_has_update_pokemon">
<option>Select Trainer</option>
<?php
$query = "SELECT name FROM Trainer";

if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->bind_result($name);
while ($stmt->fetch()) {
    echo"<option>$name</option>";
}


$stmt->close();
}


?>
</select>

口袋妖怪

<select name = "type_of_update_pokemon">
</select>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){ 
    $('select[name="trainer_has_update_pokemon"]').change(function(){ // when trainer_has_update_pokemon changes
        $.ajax({
            type:"POST", //send a post method
            url:'pkmn_dropdown.php', // path to ajax page
            data:"trainer_name="+$(this).val(), //set trainer_name to value
            success:function(response){ // retrieve response from php
                $('select[name="type_of_update_pokemon"]').html(response); // update select
            }
        });
    });
});
</script>


<select name="nickname_of_update_pokemon">
</select>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){ 
    $('select[name="type_of_update_pokemon"]').change(function(){ // when trainer_has_update_pokemon changes
        $.ajax({
            type:"POST", //send a post method
            url:'nickname_dropdown.php', // path to ajax page
            data:"pkmn_name="+$(this).val() & "trainer_name=" +$trainer_name,//set trainer_name to value
            success:function(response){ // retrieve response from php
                $('select[name="nickname_of_update_pokemon"]').html(response); // update select
            }
        });
    });
});
</script>

名称下拉列表的 php:

<?php

//connect to db

    $trainer_name = $_POST['trainer_name']; 
    $query = "SELECT DISTINCT p.name FROM Pokemon p WHERE p.owner_id = (SELECT t.trainer_id FROM Trainer t WHERE t.name = '$trainer_name')";
    if ($stmt = $mysqli->prepare($query)) {
        $stmt->execute();
        $stmt->bind_result($pkmn_name);
        while ($stmt->fetch()) {
            echo"<option>$pkmn_name</option>";
        }
        $stmt->close();
        echo $trainer_name;
    }?>

昵称下拉列表的 php:

<?php

//connect to db

$pkmn_name = $_POST['pkmn_name']; 
$query = "SELECT p.nickname FROM Pokemon p, Trainer t WHERE p.name = '$pkmn_name AND p.owner_id = t.trainer_id AND t.name = $trainer_name";
if ($stmt = $mysqli->prepare($query)) {
    $stmt->execute();
    $stmt->bind_result($nickname);
    while ($stmt->fetch()) {
        echo"<option>$nickname</option>";
    }
    $stmt->close();
}?>

任何想法如何从第一个下拉框中获取选定的培训师姓名,以便我可以在昵称下拉框中使用它

4

1 回答 1

1

运行上述代码时究竟发生了什么?昵称选择是否会丢失所有选项?您的 ajax 响应是否包含预期的选项字符串?

修改 select 的 innerHTML 在 IE 中不起作用。您将需要添加实际元素。尝试这样的事情。

$( 'select[name="nickname_of_update_pokemon"]' ).empty().append( $( response ) );

在你的 ajax 成功函数中。

于 2012-11-27T23:53:04.670 回答