views:

35

answers:

1

I have 2 select boxes, "#Country" and "#StateAbbreviation" if a user selects a country other than USA - .val="USA" in the #Country select box, I need to change the value in "#StateAbbreviation" to OTHER/INTL. - val="IT" and make it disabled.

Why doesn't this work?

$("#Country").change(function() {
  if($(this).val() != 'USA') {
    $("#StateAbbreviation").val() == 'IT'.attr("disbled", true);   
  }
});

Kane Leins

+1  A: 

You need to change it a bit, like this:

$("#Country").change(function() {
  if($(this).val() != 'USA') {
    $("#StateAbbreviation").val('IT').attr("disabled", true);   
  } else {
    $("#StateAbbreviation").val('').attr("disabled", false); 
  }
});

When setting a value, the call is .val(newValue), otherwise you're trying to set a function to a string, and call a function that doesn't exist on a string...causing a few errors.

This also resets the #StateAbbreviation <select> and re-enables if it is "USA".

Nick Craver
the `else` portion of that `if` should be `$("#StateAbbreviation").removeAttr('disabled').val('');`
gnarf
@gnarf - totally forgot that half of the operation, thanks! and updated :)
Nick Craver
Seems to work fine, thanks guys. How would I make it so that if it's not USA or CAN?? IE$("#Country").change(function() { if($(this).val() != 'USA' || 'CAN') { $("#StateAbbreviation").val('IT').attr("disabled", true); } else { $("#StateAbbreviation").val('SS').attr("disabled", false); }});do I need to make USA and CAN an array and check against that array?
Dirty Bird Design
@downvoter - Helps to say why, otherwise the answer never gets improved...
Nick Craver
@Dirty - Like this: http://jsfiddle.net/zQXxm/1/ :)
Nick Craver
@Nick - how would you check it against an array var FOO = new Array = ("USA","CAN")?
Dirty Bird Design
@Dirty - In the link in my last comment, just replace the in-line `['USA', 'CAN']` with `FOO` or whatever the array's called.
Nick Craver
@Nick - It works, thanks. Unfortunately it's clashing with another validator rule, I need to modify this $.validator.addMethod( "ReutersNA", function(value, element) { var selectedCountry = $("#Country").val(); var NorthAmerica = new Array("USA","CAN","MEX"); if($.inArray(selectedCountry,NorthAmerica) > -1) { return true; } else return false; }, "Cannot select Reuters News outside of North America." ); so it only fires if "IQPRE and or IQBAS" is checked, when I use what you did, it returns the error for the above. what a mess!
Dirty Bird Design
@Dirty Bird Design - just FYI `new Array("USA", "CAN", "MEX")` is the same as `["USA", "CAN", "MEX"]` -- might as well save some keystrokes...
gnarf