<script stylesheet="text/javascript">
var statename;
function confirmReview(formObj){
function myState( val ){
statename = val;
alert( "This is " + statename );
}
postcode = document.getElementById("postcode");
//Folowing code
if( statename== "Selangor" ){
if( postcode < 10000 || postcode > 40000 )
alert( "Invalid postcode for " + statename );
}
}
</script>
<body>
<form name = reviewform onsubmit="return confirmReview(reviewform)" method=POST>
<select id='state' onchange="myState(this.value);">
<option>Specify state or territory</option>
<option value="Selangor">Selangor</option>
<option value="Terengganu">Terengganu</option>
</select>
</form>
</body>
my question is how to store the value and save it to the statename as i fail to run in my HTML javascript code. can anyone teach?
i cannot print out my statename example if i want to make some comparison for my postcode when i load the script it run to the funciton . my HTML will just clear all the data like refresh the page without showing anything
after i edited the things senior suggested . it's still same. it cannot go to the alert that validation code with the statename
-
Where exactly do you want to store the value? In a javascript variable on the same page, or on the server for future reference?Vivek Ghaisas– Vivek Ghaisas2013年05月12日 08:36:56 +00:00Commented May 12, 2013 at 8:36
-
well your code is right , all you have to do is to wrap javascript in script tagsSatya– Satya2013年05月12日 08:37:37 +00:00Commented May 12, 2013 at 8:37
-
Please use uppercase i's and start sentences with uppercase letters. And use a more descriptive title? Thanks!Arjan– Arjan2013年05月12日 09:37:27 +00:00Commented May 12, 2013 at 9:37
-
already edited. can u see the latest version/?Chirs Lim– Chirs Lim2013年05月12日 09:53:04 +00:00Commented May 12, 2013 at 9:53
-
Your edits did not change things like "my question is" into the proper "My question is", nor changed "as i fail" into "as I fail". And PLEASE, write "can you" instead of "can u". Thanks! Also, the title is still not describing the problem at all, but I guess it's difficult to describe the problem as it's actually to broad for this site. Questions like yours are not really suitable for a Q&A site like Stack Overflow is.Arjan– Arjan2013年05月12日 11:09:54 +00:00Commented May 12, 2013 at 11:09
1 Answer 1
You forgot the script tags:
<script type="text/javascript">
var statename;
function myState( val ){
statename = val;
}
postcode = document.getElementById("postcode");
</script>
Sign up to request clarification or add additional context in comments.
6 Comments
imulsion
@alex23 oops, sorry, forgot about that xD. Thanks for editing :)
Quentin
@alex23 — If there are any such browsers, they are no longer in use. Browsers have defaulted to assuming JS forever and HTML 5 makes the attribute explicitly optional.
Chirs Lim
i cannot print out my statename example if i want to make some comparison for my postcode example
Arjan
@Chirs, put the validation code (the code with the alert) in the
myState function as well.Arjan
@Chirs, now the
alert part is within function confirmReview(formObj) (good, I guess), but function myState is also defined within function confirmReview(formObj), hence will no longer be found by onchange="myState(this.value);" Also note that confirmReview should return true or false. See Show me a Simple javascript onSubmit handler. PLEASE NOTE: by changing your question, you've also made imulsion's answer outdated. That's not nice. Note we're not a forum; please read About and FAQ. |
default