我有两个下拉菜单,输入相同。所以" thuisteam1"可以和" uitteam1"相同。但这不是我想要的。我想要你选择" thuisteam1"你不能在" uitteam1"。
上选择同一个团队的团队我尝试了一些javascript,但这对我没用。我希望它可以在PHP中完成。或者是一个可行的JavaScript。
希望你们能帮忙。
这是我的代码:
<form id="mySubmitForm" action="e2admin.php" method="post">
<select name="thuisteam1">
<option value=""></option>
<?php
mysql_data_seek($result, 0);
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['Team'] . '">' . $row['Team'] . '</option>';
}
}
?>
</select>
<input onKeyPress="return alpha(event)" style="width:20px; text-align:center;" type="text" maxlength="2" class="form-control" name="scorethuis1" placeholder="0" />
-
<input onKeyPress="return alpha(event)" style="width:20px; text-align:center;" type="text" maxlength="2" class="form-control" name="scoreuit1" placeholder="0" />
<select name="uitteam1">
<option value=""></option>
<?php
mysql_data_seek($result, 0);
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['Team'] . '">' . $row['Team'] . '</option>';
}
}
?>
</select><br>
<input style="margin-left:330px;" type="submit" class="form-control" value="Toevoegen" />
</form>
答案 0 :(得分:2)
这个简单的jquery可以帮助您检测所选内容是否相同:
$( "select" ).change(function () {
if ($( "select[name='thuisteam1'] option:selected" ).text() == $( "select[name='uitteam1'] option:selected" ).text()) {
$("option:selected").removeAttr("selected");
alert('can not select the same');
};
});
我试过这段代码并且有效。
这里是所有示例代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<select name="thuisteam1" multiple="multiple">
<option>team 1</option>
<option>team 2</option>
<option>team 3</option>
</select>
<select name="uitteam1" multiple="multiple">
<option>team 1</option>
<option>team 2</option>
<option>team 3</option>
</select>
<script>
$( "select" )
.change(function () {
if ($( "select[name='thuisteam1'] option:selected" ).text() == $( "select[name='uitteam1'] option:selected" ).text()) {
$("option:selected").removeAttr("selected");
alert('same');
};
});
</script>
</body>
</html>
答案 1 :(得分:0)
您可以发送表单并在Server PHP Way上验证
或者您在Browser JS Way中验证
在Jquery中:
$('#<idselecttwo>').bind('change' function(){
var valueOne = $('#<idselectone>').val();
var valueTwo = $('#<idselecttwo>').val();
if(valueOne == valueTwo)
{
alert('error');
}
});
答案 2 :(得分:0)
根据要求,我从你的代码中做了一个例子。我没有测试过,但那是我的想法。此代码假定e2admin.php是完全相同的文件。
<?php
$send=$_POST['send'];
if($send==1)
{
$team1=$_POST['thuisteam1'];
$team2=$_POST['uitteam1'];
if(isset($_POST['submitbutton']))
{
//Do your stuff
}
}
echo '
<form id="mySubmitForm" action="e2admin.php" method="post">
<input type="hidden" name="send" value="1">
<select name="thuisteam1" onchange="this.form.submit();">
<option value=""></option>
';
mysql_data_seek($result, 0);
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
if($team1 != $row['Team'])
{
echo '<option value="' . $row['Team'] . '">' . $row['Team'] . '</option>';
}
}
}
echo '
</select>
<select name="uitteam1" onchange="this.form.submit();">
<option value=""></option>
';
mysql_data_seek($result, 0);
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
if($team2 != $row['Team'])
{
echo '<option value="' . $row['Team'] . '">' . $row['Team'] . '</option>';
}
}
}
echo '
</select><br>
<input style="margin-left:330px;" type="submit" name="submitbutton" class="form-control" value="Toevoegen" />
</form>
';
?>
编辑:在继续操作之前,您可能希望为提交按钮指定名称并设置chech。这样,只有在实际按下提交按钮时才会继续。