简体   繁体   中英

Why doesn't my “IF” statement work? (Works if commented out)

So I'm new to jQuery, and I can't figure out why my IF statement isn't working, although code works when I comment out the IF. Please help me...Thanks!

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("tr.anon").hide();
$("#hideID").change(function(){
//    IF ($("#hideID option[value='no']").text()){
      $("tr.anon").show();
//      alert($this).find("option:selected").text()+' clicked!');
//  }ELSE{
//    $("tr.anon").hide();
//  }
  });
});
</script>
</head>
<body>
<p>Would you like to remain anonymous?&nbsp;<select id="hideID">
<option value="yes">Yes</option>
<option value="no">No</option>
</select></p>
<table border="1px">
<tr class="anon">
<td>Enter your name:</td>
<td><input class="field2" type="text" /></td>
</tr>
</table>
</body>
</html>

Thanks for the quick feedback... I have adjusted my code to the following but it still is not working.

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script><script type="text/javascript">
$(document).ready(function(){
  $("tr.anon").hide();
    $("#hideID").change(function(){
//      if ($("#hideID option[value='no']").text()){
        $("tr.anon").show();
//        alert(#hideID).find("option:selected").text()+' clicked!');
//    }else{
//      $("tr.anon").hide();
//    }
  });
});
</script>
</head>
<body>
<p>Would you like to remain anonymous?&nbsp;
<select  id="hideID">
<option value="yes">Yes</option>
<option value="no">No</option>
</select></p>
<table border="1px">
<tr class="anon">
<td>Enter your name:</td>
<td><input class="field2" type="text" /></td>
</tr>
</table>
</body>
</html>

你不应该使用CAPS进行IFELSE

if and else must be lower-case. Javascript is case-sensitive.

Also, I don't think the if clause here is doing what you want. You are testing the truthiness of #hideID option[value='no'] , which will always evaluate to true. I think you really want to check whether the 'no' value is selected. Also, you are simply toggling visibility, so jQuery's toggle method would be more appropriate here.

Try this instead:

$('tr.anon').toggle( $("#hideID option:selected").val() === "no" );

还要确保你没有在大写字母中写下'if ... else'语句!

Javascript is case sensitive, your if and else should be lowercase.

Also, you're checking whether $("#hideID option[value='no']").text() is truthy or not. If the text is anything other than an empty string you will go down the true path, is this your intention?

Ok, let's try this... I fixed the script to work. You can see the changes.

<script type="text/javascript">
$(document).ready(function(){
$("tr.anon").hide();
$("#hideID").change(function(){
    if ($("#hideID").val()=="no"){
        $("tr.anon").show();
        alert($(this).find("option:selected").text() + ' clicked!');
    } else {
        $("tr.anon").hide();
                    $("#textFieldID").val("");
    }
  });
});
</script>

Since you just have the two values, you can use this:

Demo: http://jsfiddle.net/ThinkingStiff/eqxCa/

Script:

$( 'tr.anon' ).hide();
$( '#hideID' ).change( function() { $( 'tr.anon' ).toggle(); } );

Change to value==='no'. = is for assignment, and == and === are comparison.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM