gpt4 book ai didi

javascript - 如何在ajax中选择下拉列表后更改输入类型文本值

转载 作者:行者123 更新时间:2023-11-28 02:25:07 25 4
gpt4 key购买 nike

从下拉列表中选择数据后,我试图用数据库中的数据更改输入类型文本中的值

但是数据没有进入文本输入值

<?php
//Include the database configuration file
include 'dbConfig.php';

//Fetch all the country data
$query = $db->query("SELECT * FROM countries WHERE status = 1 ORDER BY country_name ASC");

//Count total number of rows
$rowCount = $query->num_rows;
?>
<select id="country">
<option value="">Select Country</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['country_id'].'">'.$row['country_name'].'</option>';
}
}else{
echo '<option value="">Country not available</option>';
}
?>
</select>

<div id="state">

</div>


<?php
//Include the database configuration file
include 'dbConfig.php';

if(!empty($_POST["country_id"])){
//Fetch all state data
$query = $db->query("SELECT * FROM states WHERE country_id = ".$_POST['country_id']." AND status = 1 ORDER BY state_name LIMIT 1");

//Count total number of rows
$rowCount = $query->num_rows;

//State option list
if($rowCount > 0){
$row = $query->row();
echo '<input type="text" id="state" value="'.$row['state_name'].'">';
}
}
?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'index.php',
data:'country_id='+countryID,
success:function(html){
$('#state').html(html);
}
});
}
});

});
</script>

我希望根据数据库中的数据输出

这是我的数据库:

数据库国家

1

数据库状态

2

错误

3

最佳答案

试试这个

 $('#state').val(html);

 $('#state').text(html);

关于javascript - 如何在ajax中选择下拉列表后更改输入类型文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54720681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com