I need to figure out how to add more values than just t开发者_JS百科he 33498, any ideas?
function checkThis(){
var val = document.getElementById('myText').value;
var myForm = document.getElementById('myForm');
if(val=="33498")
myForm.action="url1.html";
else
myForm.action="url2.html";
}
http://www.w3schools.com/js/js_switch.asp
switch (val) {
case "33498":
myForm.action="url1.html";
break;
case "xxx":
case "yyy":
myForm.action="another.html";
break;
default:
myForm.action="url2.html";
}
function checkThis() {
var val = document.getElementById('myText').value;
var myForm = document.getElementById('myForm');
if (val == "33498") {
myForm.action = "url1.html";
} else if (val == "this") {
myForm.action = "url2.html";
} else if (val == "that") {
myForm.action = "url3.html";
} else if (val == "the other") {
myForm.action = "url4.html";
} else {
// if all else fails
}
}
You're said, you're using jQuery....
So document.getElementById('myText').value;
can simply be: $('#myText').val();
...and document.getElementById('myForm');
can simply be: $('#myForm')
.
You're also missing a variety of brackets, { }
, after your if()
statement.
You would add more conditionals to an if()
statement by using the else if
, like this...
function checkThis(){
var val = $('#myText').val();
var myForm = $('#myForm');
if(val=="33498") {
myForm.action="url1.html";
} else if (val == "568") {
myForm.action="url3.html";
} else if (val == "999") {
myForm.action="url4.html";
} else {
myForm.action="url2.html"; // <--do this one if nothing else matches
}
}
精彩评论