0

My HTML code

Transportation:
<input type="radio" name="transportation" value="BUS">  Bus </option><br>
<input type="radio" name="transportation" id="khaschkbox" value="KHAS">Khas</option><br>

Bus:
<select id="busDropdown" name="bus">
      <option value="Null"> NA </option>
    <?php
    $sql = "SELECT busNumber FROM Bus";
    $result = $conn->query($sql);
    $row_count = $result->rowCount();
    if ($row_count > 0){
    while ($row = $result->fetch())
      {
        echo "<option value=".$row['busNumber'].">".$row['busNumber']."</option>";
      }
    }
    ?>
    </select><br><br>

in my jQuery, if the user checked Khas, the dropdown list will be disabled. that works. but the problem if the user chose khas then Bus the dropdownlist will remain disabled which is wrong!

JQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
$(function() {
          $("#khaschkbox").change(function(){
            if(this.checked){
              $("#busDropdown").prop("disabled",true);
            } else {
              $("#busDropdown").prop("disabled",false);
            }
            });
        });

I need the list to appear when the user choose Bus and disappear if he chose Khas. How can I fix that?

2 Answers 2

1

You may try this : -

$(function() {
      $('[name="transportation"]').click(function(){
        if(this.value !== 'BUS'){
          $("#busDropdown").prop("disabled",true);
        } else {
          $("#busDropdown").prop("disabled",false);
        }
      });
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Well, yes, the change event is only fine with select tag, so you need to target the radio group to see what is selected....Cheers!!!
I see now, Thanks again!
1
$(function() {

var khaschkbox = $('#khaschkbox');
          $('input[name="transportation"]').change(function(){
            if(khaschkbox.is( ":checked" )){
              $("#busDropdown").attr("disabled","disabled");
            } else
              $("#busDropdown").removeAttr("disabled");
            }
            });
        });

2 Comments

Still the same problem :( could that be from my included library? <script src="ajax.googleapis.com/ajax/libs/jquery/2.1.3/…>
great. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.