user5789297 user5789297 - 4 months ago 11
jQuery Question

Multiple select input must be selected

I have two select input, the two must be selected before the #GO button shows.

( dbname,user) <--- the two select input, choose what database and choose a user to access the database.

The problem with this query is that after choosing one of the two the #GO button will show. The two must be selected before the #GO shows, if not then the #GO button hides.

the script file:

<script type="text/javascript">

$(document).ready(function() {

$("select").on('change',function() {


var x = $('select[name=name]').val();
var y = $('select[name=dbname]').val();

if (x == 0 && y == 0) {
$("#GO").hide();
} else if (x == 1 && y == 0) {
$("#GO").hide();
} else if (y==0 && x==1) {
$("#GO").hide();
}else{
$("#GO").show();
}
});


this the php file:

<?php echo "<select name='name' id='name'>";
$res = $conn->query("SELECT user FROM mysql.user");
echo '<option value="0">Select User</option>';
while($row = $res->fetch_assoc()) {
echo '<option value="' . $row['user'] . '">' . $row['user'] . '</option>'; }
echo '</select>';
echo "<br/>";

?>

<br>

<?php echo "<select name='dbname' id='dbname'>";
$res = $conn->query("show databases");
echo '<option value="0">Select Database</option>';
while($row = $res->fetch_assoc()) {
echo '<option value="' . $row['Database'] . '">' . $row['Database'] . '</option>'; }
echo '</select>';
echo "<br/>";

?><br>


<input type="submit" name="GO" value="GO" id="GO" class="GO">

Answer

what you want is to use "||" which means "OR", instead of "&&" which means "AND".

In this case, if any of them are 0, it will be hidden.

if (x == 0  || y == 0) {
  $("#GO").hide();
}else{
  $("#GO").show();
}

You can also do it like this:

//It checks whether both x and y are Not 0, if true, shows "#GO", else, stays hidden.

if (x != 0  && y != 0) {
  $("#GO").show();
}else{
  $("#GO").hide();
}