简体   繁体   中英

How can i add multiple conditions

How can I add multiple conditions in JavaScript? I don't want to check with the && , I want to know if there is a way like elseif or I don't know, to check all conditions if the first one is false then go to another and if that too is false go to else . In my example if I write 'blank' in the text field it will show me nothing. The code won't work but if I delete the code from the elseif (*) part is going to work just fine. Why my elseif doesn't work? I tried to write something different than "blank" to check my elseif condition too, but I don't know why it doesn't work. This is my code:

function Test() {
  var test = document.getElementById('name').value;
  var para = document.getElementById('hidden');

  if (test == "blank") {
    document.write(test.length);
  }
  ( * ) - > elseif(test = "Cristian") {

    para.innerHTML = "this is first para";
  } < -( * )
  else {
    para.innerHTML = "this is second para";
    para.style.font = "italic bold 20px arial,serif";


  }
}
<p id="hidden">Surprise!</p>

<input type="text" id="name" />

<input type="button" onclick="Test()" value="press me" />

You were close...

Instead of writing elseif you should add a space and write else if

AND

To compare element you should use == or === .

= is used for initialization

You can also use the switch -statement:

switch(test){
  case: "test":
    document.write(test.length);
    break;
  case: "Christian":
    para.innerHTML = "this is first para";
    break;
  default:
    para.innerHTML = "this is second para";
    para.style.font = "italic bold 20px arial,serif";
}

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