我有2个下拉菜单,第二个下拉菜单是根据第一个下拉菜单填充的,问题是当按Tab键或提交按钮时,第二个下拉菜单的值会自动更改它自己或显示为空。
脚本:
function ajaxfunction(parent) {
$(document).ready(function() {
$('.control').change(function() {
var Value = $(this).val();
if (Value != null) {
jQuery.ajax({
type: 'POST',
url: "name.php?comp_type=" + Value,
success: function(data) {
$('#name').html(data);
}
});
}
})
})
}
HTML:
<!-- first dop down menu -->
<select name="type" class="control" onchange="ajaxfunction(this.value)">
<option value="Electronice">Electroinc</option>
<option value="Fashion">Fashion</option>
<option value="Education">Electroinc</option>
</select>
<!-- second drop down menu -->
<select id="name" name="name"></select>
抱歉我的英文
答案 0 :(得分:1)
您应该在加载页面时调用该函数
<script>
$(document).ready(function() {
ajaxfunction();
});
function ajaxfunction(parent){
$('.control').change(function(){
var Value = $(this).val();
if(Value != null){
jQuery.ajax({
type: 'POST',
url: "name.php?comp_type="+Value,
success: function(data){
$('#name').html(data);
}
});
}
})
}
</script>
答案 1 :(得分:0)
保持所有,只需更新您的代码,如下所示
$(document).ready(function() {
$('.control').change(function() {
var Value = $(".control").val();
if (Value != null) {
jQuery.ajax({
type: 'POST',
url: "name.php?comp_type=" + Value,
success: function(data) {
$('#name').html(data);
}
});
}
})
});
和html如下:
<!-- first dop down menu -->
<select name="type" class="control">
<option value="Electronice">Electroinc</option>
<option value="Fashion">Fashion</option>
<option value="Education">Electroinc</option>
</select>
<!-- second drop down menu -->
<select id="name" name="name"></select>
答案 2 :(得分:0)
您可以使用其他选项以此方式显示下拉服务。您可以使用ajax throw显示内容。
test.php的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="http://code.jquery.com/jquery.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
$("select.country").change(function(){
var selectedCountry = $(".country option:selected").val();
$.ajax({
type: "POST",
url: "process-request.php",
data: { country : selectedCountry }
}).done(function(data){
$("#response").html(data);
});
});
});
</script>
</head>
<body>
<form>
<table>
<tr>
<td>
<label>Country:</label>
<select class="country">
<option>Select</option>
<option value="usa">United States</option>
<option value="india">India</option>
<option value="uk">United Kingdom</option>
</select>
</td>
<td id="response"></td>
</tr>
</table>
</form>
</body>
</html>
过程request.php
<?php
if(isset($_POST["country"])){
$country = $_POST["country"];
$countryArr = array("usa" => array("New Yourk", "Los Angeles", "California"), "india" => array("Mumbai", "New Delhi", "Bangalore"),
"uk" => array("London", "Manchester", "Liverpool"));
if($country !== 'Select'){
echo "<label>Services:</label>";
echo "<select>";
foreach($countryArr[$country] as $value){
echo "<option>". $value . "</option>";
}
echo "</select>";
}
}
?>