我有一组下拉菜单(HTML选择),这些菜单中填充了来自mysql查询的相同值。我想,只要我从下拉列表中选择一个选项,就不能在其余选项中选择该选项(或者显示为禁用)。基本上,这就是我所拥有的:
<form name="test" method="post" action="confirmSelection.php">
<select name="color1" id="color1">
<?php
$sql = "SELECT *
FROM colors
ORDER BY name";
$res = mysql_query($sql);
while( $row = mysql_fetch_array( $res ) )
{
?>
<option value="<?php echo ($row["id"]) ?>"> <?php echo( $row["name"] )?></option>
<?php
}
?>
</select>
<select name="color2" id="color2">
<?php
$sql = "SELECT *
FROM colors
ORDER BY name";
$res = mysql_query($sql);
while( $row = mysql_fetch_array( $res ) )
{
?>
<option value="<?php echo ($row["id"]) ?>"> <?php echo( $row["name"] )?></option>
<?php
}
?>
</select>
<select name="color3" id="color3">
<?php
$sql = "SELECT *
FROM colors
ORDER BY name";
$res = mysql_query($sql);
while( $row = mysql_fetch_array( $res ) )
{
?>
<option value="<?php echo ($row["id"]) ?>"> <?php echo( $row["name"] )?></option>
<?php
}
?>
</select>
有什么想法吗?我想它一定很简单,但我真的不知道如何寻找它(我很难为这个咨询找到合适的标题......)。
提前多多感谢。
编辑:
ajax代码:
function showData(dataType)
{
var capa=document.getElementById("content");
var ajax=nuevoAjax();
capa.innerHTML="";
ajax.open("POST", "test.php", true);
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("d="+dataType);
ajax.onreadystatechange=function()
{
if (ajax.readyState==4)
{
capa.innerHTML=ajax.responseText;
}
}
}
在test.php中我有4,5和6个选择器,就像我解释的那样3.我也有这个:
....
$dataType=$_POST['d'];
if($dataType=='1')
{
//4 selectors
}elseif($dataType == '2')
{
//5 selectors
}elseif($dataType == '3')
{
//6 selectors
}
div正在正确更新并显示正确的布局(4,5或6个选择),但是你给我的代码不起作用。我尝试在test.php和landscape.php中包含javascript。没有运气:(。
答案 0 :(得分:1)
将PHP放在一边。一旦PHP完成了它,你就会有一个纯HTML页面,对吧?因此,javascript必须像往常一样与DOM本身进行交互。
首先,我想在数组中存储所有选择元素的选定选项。这样做可以让我在第一个和第二个颜色选择器中选择一个选项,并在第三个颜色选择器中禁用它们。
然后,简单地遍历所有连接的选择(我通过匹配的类连接它们),并在该迭代中覆盖所有选定的选项,并在适当时禁用它们。
听起来很简单,但这有点挑战。更多的挑战(但不是更多)可能是允许多选。
希望它有所帮助,让我知道它是否需要改变。
// This array will be used to store the current selection of
// each connected select. They are connected by a class attr.
var selectedOption = new Array($(".colorSelector").length);
/*******
* Any time any select is changed, we update the selectedOption
* array to include the new selection. Note that the array is
* the same length as the number of selects, and that we're
* setting the value to the position in that array that relates
* to the position of the element itself. By this, I mean that
* selectedOption[0] contains the value of select[0],
* selectedOption[4] contains the value of select[4],
* and so on.
*******/
$("body").on("change",".colorSelector", function() {
// First, get the value of the selected option.
selectedOption[$(this).index()] = $(this).val();
/***
* Now, we iterate over every connected select element.
* we want to disable all values that are selected in
* all other connected selects -- those values we've stored
* in the selectedOption array. As long as the value is
* not blank, or the default '...', or the current element,
* we disable that option.
***/
$(".colorSelector").each(function() {
// First, re-enable all options.
$(this).children("option").removeAttr("disabled");
// Iterate over the selectedOption list
for (i = 0; i < selectedOption.length; i++) {
if (selectedOption[i] != "" && selectedOption[i] != "..." && i != $(this).index()) {
// Disable any option that isn't default, or
// ignore if the current selectedOption points to
// this select.
$(this).children("option[value='" + selectedOption[i] + "']").attr("disabled", "disabled");
}
}
})
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="color1" id="color1" class="colorSelector">
<option>...</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="indigo">Indigo</option>
<option value="violet">Violet</option>
</select>
<select name="color2" id="color0" class="colorSelector">
<option>...</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="indigo">Indigo</option>
<option value="violet">Violet</option>
</select>
<select name="color3" id="color3" class="colorSelector">
<option>...</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="indigo">Indigo</option>
<option value="violet">Violet</option>
</select>
&#13;
此外,这是fiddle,以防万一。
编辑:你提到当AJAX填充选择时,这不起作用。你完全正确,它没有像书面那样工作。原因是,在页面加载后加载元素时,它不会自动连接到页面的当前侦听器。相反,我已经改变了上面代码中监听器附加的位置。我没有像$(".colorSelector").on("change"...)
那样倾听,而是将其更改为$("body").on("change", ".colorSelector", function(){...});
- 请注意,听众现在已经附着在身体上了。执行此操作会导致具有.colorSelector
类的任何元素触发,无论它是最初还是稍后添加。希望这有帮助!