html中的下拉列表数组
Array of drop down list in html
如何动态更改下拉列表的值。案例如下:例如,我有一个下拉列表数组,其中有两个名称,比如name1和name2,名称1有grade,名称2也有。
我想做的是改变第一个下拉菜单,它的值是name和我想让第二个下拉菜单根据他的成绩自动改变。如:约翰-> 90
请帮我找到数组的索引和下拉菜单的选择值使用javascript。提前谢谢。
<script>
function Change2(a){
var options1= document.getElementById('stuname[]').options;
var options2= document.getElementById('stugrade[]').options;
for(i = 0; options1.length;i++){
if(options1[i].selected == true){
options2[i].selected = true;
}
}
} // what I want is, if I change the first dropdown in row 1, the 2nd dropdown should change too, and same in row 2.
</script>
<?php
$getlist= mysql_query("SELECT * FROM STUDENTS");
while($row = mysql_fetch_assoc($getlist)){
print "<select id = 'stuname[]' name = 'stuname[]' class = 'text4' onchange = 'Change2(this.value);'>";
$getname = mysql_query("SELECT * FROM NAME");
while($row = mysql_fetch_assoc($getname)){
$name = $row['name'];
print "<option value = '$name'>$name</option>";
}
print "</select>";
print"<select id = 'stugrade[]' name = 'stugrade[]' onchange = 'Change2(this.value);'>";?>
<?php
$getgrade = mysql_query("SELECT * FROM GRADE");
while($row = mysql_fetch_assoc($getgrade)){
$grade = $row['grade'];
print "<option value = '$grade'>$grade</option>";
}
print "</select><br>";
}
?> // heres my code.. edited
这是解决此类问题的常用方法,不同的是,您需要相应地更改查询和名称
JS文件
$(document).ready(function(){
getfirstSelect();
})
function getSecondSelect() {
var firstDropDownSelectedID = $('#firstSelect option:selected').val();
$.ajax({
type: "POST",
url: "getSecondDropDownOptions.php",
data: "firstDropDownSelectedID="+firstDropDownSelectedID
}).done(function (result) {
//alert(result)
$("#secondSeect").html(result);
});
}
function getfirstSelect() {
$.ajax({
type: "POST",
url: "getFirstDropDownOptions.php",
data: {}
}).done(function (result) {
//alert(result)
$("#secondSeect").html(result);
});
}
getSecondDropDownOptions.php
<?
$firstDropDownSelectedID = isset($_POST['firstDropDownSelectedID']) ? (int)$_POST['firstDropDownSelectedID'] : 0;
$list2 = mysql_query("SELECT * FROM LIST2 WHERE relatedid=".$firstDropDownSelectedID);
$returnString = "";
while($row = mysql_fetch_assoc($list2)){
$returnString.= "<option value = '".$row['grade']."' >".$row['grade']."</option>";
}
print $returnString;
?>
getFirstDropDownOptions.php
<?
$list1 = mysql_query("SELECT * FROM LIST1 ");
$returnString = "";
while($row = mysql_fetch_assoc($list1)){
$returnString.= "<option value = '".$row['grade']."' >".$row['grade']."</option>";
}
print $returnString;
?>
相关文章:
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 从多维嵌套json数组创建下拉列表
- Jquery 读取编号组中选择下拉列表的数组
- Knockout JS中具有下拉列表的数组
- 由vbscript下拉列表填充的经典asp-javascript数组
- 如何从下拉列表中由可观察数组填充的选定项中检索完整对象
- 从下拉列表中的数组中选取某个项目,然后将其显示为第一项并按字母顺序显示
- jQuery 根据数组值隐藏下拉列表的选项
- 用下拉列表填充数字数组
- 从 xml 文件创建一个下拉列表,不带重复值.我需要数组吗?
- 从数组 Jquery 中获取下拉列表的选定索引
- 在 JQuery 验证中验证多个“数组命名”文件输入和下拉列表
- 从 Javascript 和 Java 返回的数组中填充下拉列表
- 如何将下拉列表中的所有选项添加到 php 中的数组中
- 数组循环 - 处理下拉列表中的空值
- 挖空可观察数组下拉绑定问题
- 使用 javascript 使用 PHP 数组中的选项填充动态创建的下拉列表
- 将PHP数组输入到Javascript数组/下拉列表中
- 如果一组下拉列表中的任何下拉列表值与其他下拉列表选择的值相等,则应删除整个其他下拉列表
- 显示XSL和PDF导出以及允许我们选择要显示的行数的下拉列表