第二个函数忘记了所选值
Second function has forgotten the selected value
我正在尝试使用 ajax 函数从数据库中获取数据。但是代码的某些部分不起作用。$car_id, witch 是下拉列表中的选定值,在函数的第二部分中被遗忘。
首先。我有一个下拉菜单,用户可以在其中选择汽车:
<select name="car_id" id="car_id" onChange="getCar(this.value)">
<option value="0">Select</option>
<option value="1">Volvo XC90</option>
<option value="2">Saab 95</option>
<option value="3">Mercedes SLK</option>
<option value="4">Audi TT</option>
</select>
选择(onChange事件)后,我的脚本运行以下函数:
<script>
function getCar(selectedItem) {
jQuery.ajax({
url: 'get1.php',
method: 'POST',
data: {'car_id' : jQuery('#car_id').val()},
success: function(response){
jQuery('#car_id').val(response);
jQuery('#car_brand').val(response);
getCar2()
},
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>
此函数运行以下 SQL 查询: SELECT car_brand FROM cars WHERE car_id = $car_id
该函数从数据库中获取car_brand
,并将其发布到#car_brand
文本字段中。这部分代码工作正常。但如您所见,该函数调用第二个函数:
<script>
function getCar2(selectedItem) {
jQuery.ajax({
url: 'get2.php',
method: 'POST',
data: {'car_id' : jQuery('#car_id').val()},
success: function(response){
jQuery('#car_id').val(response);
jQuery('#car_model').val(response);
},
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>
这次函数尝试获取car_model
。该函数需要将其发布到#car_model
文本字段中。但这行不通。当我查看Firebug控制台时,我看到第二个函数getCar2
忘记了$car_id。
当我选择值 3 时,第一个函数运行 SQL 语句: SELECT car_brand FROM cars WHERE car_id = 3
第二个功能: SELECT car_model FROM cars WHERE car_id =
如何解决这个问题?
更新 1:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error) ;
}else {
$car_id = isset($_POST['car_id'])?$_POST['car_id']:'';
$car_brand = isset($_POST['car_brand'])?$_POST['car_brand']:'';
$query = 'SELECT * FROM cars WHERE car_id="' . mysqli_real_escape_string($conn, $car_id) . '"';
$res = mysqli_query($conn, $query) ;
if (mysqli_num_rows($res) > 0) {
$result = mysqli_fetch_assoc($res) ;
echo $result['car_brand'];
}else{
$result = mysqli_fetch_assoc($res) ;
echo "";
}
}
?>
从getCar中删除jQuery('#car_id').val(response);
。 无需更新它,因为它的一切都准备好了car_id
<script>
function getCar(selectedItem) {
jQuery.ajax({
url: 'get1.php',
method: 'POST',
data: {'car_id' : jQuery('#car_id').val()},
success: function(response){
jQuery('#car_brand').val(response);
getCar2()
},
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>
首先,您将#car_id
重新分配给函数 getCar()
中的新值,并带有以下行:jQuery('#car_id').val(response);
。
您正在为两个 dom 元素分配与第一个函数 ajax 回调中response
相同的值。我不知道这是否是你打算做的。您正在通过 selectedItem
变量向 getCar()
函数传递select
元素的实际值。如果您确实需要重新分配 #car_id
的值,则应将 selectedItem
变量传递给 getCar2
函数,该函数仍应包含最初选择的值,并在 getCar2
函数中使用它。
否则,从 getCar
函数中删除行jQuery('#car_id').val(response);
,也可以删除变量对两个函数的传递,因为您没有在其他任何地方使用它们。
- Meteor忘记了密码的实现
- CKEditor小部件忘记了它们'重新组装小部件,变得毫无用处
- angular 1.3中的ng选项在集合异步设置时忘记了ng模型
- ReferenceError:即使定义了函数,也没有定义函数
- Knockoutjsobservable和jQuery扩展了函数
- JavaScript 立即调用了函数模式差异
- 多选在继续后忘记了选定的值
- Javascript立即调用了函数模式
- 第二个函数忘记了所选值
- IE 版本 10.0.9200 “忘记”了 CSS 样式
- Jquery文档准备好了函数声明
- Ajax 请求减慢了函数的其余部分,如何修复
- 变量被分配了函数
- 使用jQuery检查哪个事件触发了函数
- Javascript替换了函数说明
- js在参数中命名了函数,不能访问出函数
- GWT和JSNI.检查是否定义了函数
- 尽管未执行代码块,但仍定义了函数
- 如何确定哪个按钮调用了函数
- jQuery重构了函数