gpt4 book ai didi

javascript - 依赖下拉 html,允许网页上使用不同的值

转载 作者:行者123 更新时间:2023-11-29 21:32:58 25 4
gpt4 key购买 nike

我被困在我的网站上。我的 HTML 网页上有 3 个由 mysql 填充的下拉列表。例如,在我的表格中,一列中有 5 个字段(选项 1选项 2选项 3选项 4选项 5)。在我的下拉列表中,我希望能够从下拉列表 A 中选择选项 1,然后在下拉列表B和C;如果我将其更改为选项 2,则选项 1 应该在下拉列表 B 和 C 中可用 - 依此类推。

有办法这样做吗?我一直在尝试 php 和 javascript,但没有任何运气。

下面是我已有的代码 - 仍然失败

html

    <!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function()
{
$(".Qual1").change(function()
{
var id=$(this).val();
var Userid=
<?php
$connection = mysql_connect("localhost","root","") or die ("Error connecting to mysql server: ".mysql_error());
mysql_select_db("mytestdb", $connection) or die ("Error selecting specified database on mysql server: ".mysql_error());

$log_user = $_SESSION['login_user'];
$sqlquery = mysql_query("select fnGetUserID('$log_user') as UserID", $connection);
$row = mysql_fetch_assoc($sqlquery);

echo $row['UserID'];
?>;
var Circuitid =
<?php
$connection = mysql_connect("localhost","root","") or die ("Error connecting to mysql server: ".mysql_error());
mysql_select_db("mytestdb", $connection) or die ("Error selecting specified database on mysql server: ".mysql_error());

$race_name = $_SESSION['Race'];
$sqlquery = mysql_query("select fnGetCircuitID('$race_name') as RaceID", $connection);
$row = mysql_fetch_assoc($sqlquery);

echo $row['RaceID'];
?>;

<?php echo json_encode($_SESSION['Race']); ?>;
var dataString = 'id='+id+'&Userid='+Userid+'&Circuitid='+Circuitid;

//alert(dataString);

$.ajax
({
type: "POST",
url: "ajaxDrivers.php",
data: dataString,
cache: false,
success: function(html)
{
$(".Qual2").html(html);
}
});

});
});
</script>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="profile">
<b id="Go Back"><a href="profile.php">Go Back</a></b>
<br>
<br>
<b id="welcome">Making Picks for : <i><?php echo $_SESSION['Race'] ?></i></b>
<br>
<br>
</div>

<form action="">
<table>
<tr>
<td colspan='2'>Prediction Form</td>
</tr>
<tr>
<td>Qualifying 1:</td>
<td>
<select id="Qual1" name="Qual1" class="Qual1">

<?php

$log_user = $_SESSION['login_user'];

$connection = mysql_connect("localhost","root","") or die ("Error connecting to mysql server: ".mysql_error());
mysql_select_db("mytestdb", $connection) or die ("Error selecting specified database on mysql server: ".mysql_error());

// Get the UserID
$sqlquery = mysql_query("select fnGetUserID('$log_user') as UserID", $connection);
$row = mysql_fetch_assoc($sqlquery);
$_SESSION['intUserID'] =$row['UserID'];

// Unset Variable to be ised again
unset($sqlquery);
unset($row);

$race_name = $_SESSION['Race'];

// Get the UserID
$sqlquery = mysql_query("select fnGetCircuitID('$race_name') as RaceID", $connection);
$row = mysql_fetch_assoc($sqlquery);
$_SESSION['intRaceID'] =$row['RaceID'];

$race_id = $_SESSION['intRaceID'];
$user_id = $_SESSION['intUserID'];

$qual1 = mysql_query("CALL spGetDrivers($user_id,$race_id)");
//$rows = mysql_query($qual1) or die ("Query to get data from firsttable failed: ".mysql_error());
while($rs=mysql_fetch_array($qual1)){
echo '<option value="'.$rs['pkDriverID'].'">'.$rs["strDriverName"].'</option>';
}

?>

</select>
</td>
</tr>
<tr>
<td>Qualifying 2:</td>
<td>
<select id="Qual2" name="Qual2" class="Qual2">

<option selected="selected">--Select Driver--</option>

</select>
</td>
</tr>
</table>
</form>
</body>
</html>

php

<?php

header('Content-Type: text/html; charset=ISO-8859-15');
$connection = mysql_connect("localhost","root","") or die ("Error connecting to mysql server: ".mysql_error());
mysql_select_db("mytestdb", $connection) or die ("Error selecting specified database on mysql server: ".mysql_error());
if($_POST['id'])
{
$id=$_POST['id'];
$userID=$_POST['Userid'];
$CircuitID=$_POST['Circuitid'];

$rs=mysql_query("CALL spInTempDriver($userID,$CircuitID,$id)");

//echo '<option value="'.$CircuitID.'">'.$CircuitID.'</option>';

$sql=mysql_query("CALL spGetDrivers($user_id,$CircuitID)");

while($row=mysql_fetch_array($sql))
{
$id=$row['pkDriverID'];
$data=$row['strDriverName'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
}

?>

最佳答案

这是一个jsfiddle如果已经在另一个 select 元素中选择了 option,它会使用 jQuery 禁用它们。

JS:

$(function() {

$("select").on("change", function() { // select on change event

var selectedValues = []; // array of all the values selected by any select element
$("select > option:selected").each(function() {
var val = $(this).val();
if (val != "") { // exlude the value if it is ""
selectedValues.push(val);
}
});

$("select").not($(this)).each(function() { // for each other select element
var selectedValue = $(this).val(); // value of the currently selected option for this select element
$(this).find("option").each(function() { // for each option
if ($(this).val() != selectedValue && selectedValues.indexOf($(this).val()) > -1) { // disable only if not the currently selected option and if it is selected by a select element
$(this).hide();
} else {
$(this).show(); // else show the option if it is hidden
}

});
});

});

});

关于javascript - 依赖下拉 html,允许网页上使用不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35118158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com