Javascript 下拉列表,基于另一个下拉列表,选择“来自数据库”选项
如果之前讨论过,很抱歉,但它们都不适合我。我有5个下拉列表,它们是品牌、型号、颜色、发动机号和底盘号。我的问题是,我应该如何使该型号的下拉列表基于所选的品牌下拉列表,例如,如果用户根据我在数据库中发布的图像选择Honda well,那么Honda的BrandID为1,因此所有具有BrandID=1的车型。所示模型的下拉列表仅显示brandID=1的模型。然后,颜色的下拉列表是基于模型的下拉列表,因此与我前面讨论的逻辑相同。最后,引擎号和Chasis号的下拉列表基于颜色的下拉列表,也与我讨论的逻辑相同 这是我的品牌下拉列表代码Javascript 下拉列表,基于另一个下拉列表,选择“来自数据库”选项,javascript,php,jquery,html,drop-down-menu,Javascript,Php,Jquery,Html,Drop Down Menu,如果之前讨论过,很抱歉,但它们都不适合我。我有5个下拉列表,它们是品牌、型号、颜色、发动机号和底盘号。我的问题是,我应该如何使该型号的下拉列表基于所选的品牌下拉列表,例如,如果用户根据我在数据库中发布的图像选择Honda well,那么Honda的BrandID为1,因此所有具有BrandID=1的车型。所示模型的下拉列表仅显示brandID=1的模型。然后,颜色的下拉列表是基于模型的下拉列表,因此与我前面讨论的逻辑相同。最后,引擎号和Chasis号的下拉列表基于颜色的下拉列表,也与我讨论的逻辑
品牌
在select标记上写一个onchange事件
例如,要根据所选品牌更改型号,您应
品牌
这是一大堆数据,你希望我们为你编码吗?你能突出显示错误吗?谢谢DSo你想把你的数据放在下拉列表中吗?@Flido根据上面我的php代码,数据已经在下拉列表中,我的问题是当我选择品牌下拉列表时,例如本田,必须只显示选定的选项,而不是全部。看看我桌上的白兰地。本田的BrandID=1,因此如果我在品牌下拉列表中选择本田,则模型下拉列表的选择==BrandID=1。我真的很抱歉,但我对onchange事件和ajax并不太熟悉:(“对一些php文件进行ajax调用,该文件将返回基于品牌ID的模型并将其绑定到模型下拉列表中”如果我发现了这一点,这是可能的吗?我所有的5个下拉列表都在同一个php文件中
Write an onchange event on select tag
for e.g. to change the Models based on brand selected you should write
<label for="bName" class="control-label col-xs-4"><p class="left">Brand</p></label>
<div class="col-xs-7">
<div class="req">
<?php
include_once "config.php";
$bsql="SELECT bName, brandID FROM brand order by bName";
$bstmt=$con->prepare($bsql);
$bstmt->execute();
$bstmt->bind_result($bName, $bid);
$bstmt->store_result();
echo "<select name='brandID' class='form-control' **onchange='getModels(this)'**>
<option value=''></option>";
while ($bstmt->fetch()){
echo '<option value="'.$bid.'">'.$bName.'</option>';
}
echo '</select>';
//The function getModels(this) will get called whenever user will change the value of the brand option.
Now define this method in your js file
function getModels(BrandObj)
{
brandValue=BrandObj.value; // Will give you the ID of brand which is selected.
// Make A ajax call to some php file which will return models based on brand ID & bind it to your Models Dropdown
$.ajax({
url: 'getModels.php',
type: 'GET', // Method Type
data: 'brandID=brandValue',// This parameter will be sent to getModels.php
success: function(**data**) {
//called when successful the data we have returned in getModels.php will be accessible in "data" variable
// Decode the response & bind it to your dropdownList
},
error: function(e) {
//called when there is an error
//console.log(e.message);
}
});
}
// IN your getModels.php file write following code
$BrandID=@$_GET['brandID'];
//Connect to database
// Write a sql to find models having brand_id=$BrandID
// Fetch rows & create array of Models & return it using
echo json_encode(*your_array_name*)
// END getModels.php
// You can find the detailed documentation of AJAX
http://api.jquery.com/jquery.ajax/