如何为我的jQuery表单添加其他值

时间:2011-10-14 14:27:53

标签: javascript jquery

我需要弄清楚如何添加更多的值而不仅仅是33498,任何想法?

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";


}

3 个答案:

答案 0 :(得分:1)

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";

}

答案 1 :(得分:0)

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   

    }

}

答案 2 :(得分:0)

你说,你正在使用jQuery ....

所以document.getElementById('myText').value;可以简单地为:$('#myText').val();

...而document.getElementById('myForm');可以简单地为:$('#myForm')

{ }声明之后,您还遗漏了各种方括号if()

您可以使用if()else if语句添加更多条件,就像这样......

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
  }

}