简体   繁体   English

在javascript中使用if和else if语句?

[英]using if and else if statements in javascript?

I am trying to find a away (the correct and easy way) to use IF and else IF statements in javascript. 我试图找到一个远离(正确和简单的方法)在javascript中使用IF和其他IF语句。

here is what I am trying to do... 这是我想要做的......

at the moment, I am joining 2 input fields value and display them in another input balue like so: 此刻,我正在加入2个输入字段值并将其显示在另一个输入字母中,如下所示:

function FillIn() {
  document.getElementById('custom').value = 

'Main Text:' + ' ' +  document.getElementById('inputName').value + ', ' + '1st Text:' + ' ' + document.getElementById('inputName101').value

    ; 
}

so basically the above code will place the values of inputName and inputName101 into the custom input field. 所以基本上上面的代码会将inputNameinputName101的值放入custom输入字段中。 this works just fine. 这很好用。

Now, I have 2 radio buttons which I want to display their Values into the custom input field depending on which one is selected/ checked . 现在,我有2个单选按钮,我想将它们的值显示在自定义输入字段中,具体取决于选择/ checked那个。

for this I am using the following code: 为此,我使用以下代码:

    function FillIn() {
      document.getElementById('custom').value = 

    'Main Text:' + ' ' +  document.getElementById('inputName').value + ', ' + '1st Text:' + ' ' + document.getElementById('inputName101').value

if(document.getElementById('male').checked) {

   + ', ' + 'Type:' + ' ' + document.getElementById('male').value

}else if(document.getElementById('female').checked) {

     + ', ' + 'Type:' + ' ' + document.getElementById('female').value

}

        ; 
    }

however, i am not sure if I am doing this correctly as I don't get the values of radio buttons in the custom input field at all.. 但是,我不确定我是否正确这样做,因为我根本没有在custom输入字段中获得单选按钮的值。

Could someone please advise on this? 有人可以就此提出建议吗?

EDIT: 编辑:

HTML for radio buttons: 单选按钮的HTML:

<input id="male" type="radio" name="gender" value="Road Legal" onclick="showhidediv(this);">  

<input id="female" type="radio" checked="checked" name="gender" value="Show Plate" onclick="showhidediv(this);">

Actually, you're doing it wrong, you can't directly concatenate if / else conditions with strings, and the cleaner way to write your code would be 实际上,你做错了,你不能直接连接if / else条件和字符串,更简洁的方式编写你的代码将是

function FillIn() {
    var str = 'Main Text: ';
    str += document.getElementById('inputName').value;
    str += ', 1st Text: ';
    str += document.getElementById('inputName101').value;

    if ( document.getElementById('male').checked ) {
        str += ', Type: ';
        str += document.getElementById('male').value;
    } else if ( document.getElementById('female').checked ) {
        str += ', Type: ';
        str += document.getElementById('female').value;
    }

    document.getElementById('custom').value = str;
}

That won't work as it is... you can't just concatenate strings in and out of if statements as if it was a string. 这不会起作用......你不能只是将if语句中的字符串连接起来,就好像它是一个字符串一样。 You need to do it like this... 你需要像这样做......

function FillIn() {
  document.getElementById('custom').value = 'Main Text:' + ' ' +  
    document.getElementById('inputName').value + ', ' + '1st Text:' + ' ' + 
    document.getElementById('inputName101').value;

  if (document.getElementById('male').checked) {
    document.getElementById('custom').value += ', ' + 'Type:' + ' ' +
      document.getElementById('male').value;
  }
  else if (document.getElementById('female').checked) {
    document.getElementById('custom').value += ', ' + 'Type:' + ' ' + 
      document.getElementById('female').value;
  }
}

In fact, that most probably threw an error on console, you should check those to see what's wrong with your code. 事实上,最有可能在控制台上出现错误,你应该检查一下,看看你的代码有什么问题。

You can't use an if statement inside an expression. 您不能在表达式中使用if语句。 You have happened to write it in a way that actually runs, but the expressions inside the if statement are just evaluated and discarded. 您碰巧以实际运行的方式编写它,但if语句中的表达式只是被评估和丢弃。

You can use the conditional operator in an expression: 您可以在表达式中使用条件运算符:

function FillIn() {
  document.getElementById('custom').value = 
    'Main Text:' + ' ' +  document.getElementById('inputName').value + ', ' +
    '1st Text:' + ' ' + document.getElementById('inputName101').value +
    (document.getElementById('male').checked ?
      ', ' + 'Type:' + ' ' + document.getElementById('male').value :
      (document.getElementById('female').checked ?
        ', ' + 'Type:' + ' ' + document.getElementById('female').value :
        ''
      )
    ); 
}

Using local variables for the elements makes the code a bit more readable: 使用元素的局部变量使代码更具可读性:

function FillIn() {
  var male = document.getElementById('male');
  var female = document.getElementById('female');
  document.getElementById('custom').value = 
    'Main Text:' + ' ' +  document.getElementById('inputName').value + ', ' +
    '1st Text:' + ' ' + document.getElementById('inputName101').value +
    (male.checked ?
      ', ' + 'Type:' + ' ' + male.value :
      (female.checked ?
        ', ' + 'Type:' + ' ' + female.value :
        ''
      )
    ); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM