javascript 在 OnClick 中使用“if/else”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15370544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:34:28  来源:igfitidea点击:

Using "if/else" with OnClick

javascriptbuttononclickif-statement

提问by NollProcent

First of all, is it even possible to write if/else statements directly in html with onclick attribute? And if so, why is my code not working?

首先,是否可以直接在带有 onclick 属性的 html 中编写 if/else 语句?如果是这样,为什么我的代码不起作用?

So this is a button. The "Calc.Input.value" refers to a text input and I want to display an error message if that field is 0 or blank. In all other cases I want some other things to happen as you can see. This is not relevant though, as everything after the "else" worked fine before.

所以这是一个按钮。“Calc.Input.value”指的是文本输入,如果该字段为 0 或空白,我想显示错误消息。在所有其他情况下,我希望发生一些其他事情,如您所见。但这无关紧要,因为“else”之后的所有内容之前都可以正常工作。

<INPUT TYPE="button" NAME="drop" id="dropbutton" style="background-color:#D1E8D5;" VALUE="
Sl?pp ankare " OnClick="if ("Calc.Input.value == ''" || "Calc.Input.value == '0'")
{window.alert("Please enter a number");} else 
{document.getElementById('dropbutton').value=' Justera 
l?ngd ';document.getElementById('length').innerHTML =
Calc.Input.value;document.getElementById('dropbutton').style.cssText = 'background-
color:#FFF6B3;';}">

回答by kay - SE is evil

Just DON'T put it in the onclick attribute.

只是不要把它放在 onclick 属性中。

Use

利用

<INPUT TYPE="button" NAME="drop" id="dropbutton" style="background-color:#D1E8D5;" VALUE="Sl?pp ankare ">
<script>
document.getElementById('dropbutton').onclick = function() {
    if (Calc.Input.value == '' || Calc.Input.value == '0') {
         window.alert("Please enter a number");
    } else {
        document.getElementById('dropbutton').value=' Justera l?ngd ';
        document.getElementById('length').innerHTML = Calc.Input.value;
        document.getElementById('dropbutton').style.backgroundColor = '#FFF6B3';
    }
    return false;
}
</script>

回答by Amy

The reason your code is not working is because you need to escape your "quotation marks, otherwise it will be interpreted as the HTML attribute symbol.

你的代码不工作的原因是因为你需要对你的"引号进行转义,否则它会被解释为 HTML 属性符号。

And I agree with the others, it is a bad practice to write your JavaScript inline inside your HTML.

我同意其他人的看法,在 HTML 中内联编写 JavaScript 是一种不好的做法。

回答by washicd

sweetamylase's reply is best but you may also consider:

Sweetamylase 的回答是最好的,但您也可以考虑:

If your onclick=assignment is enclosed in double quotes (onclick="...")then try using only single quote characters inside of those double quotes (or vice versa).

如果您的onclick=赋值被双引号括起来(onclick="..."),那么请尝试在这些双引号内仅使用单引号字符(反之亦然)。

This can make things easier when you need to make assignments to the onclick event when inserting elements dynamically and having separate script functions isn't really a good option.

当您需要在动态插入元素时对 onclick 事件进行分配并且具有单独的脚本功能并不是一个好的选择时,这可以使事情变得更容易。

Example:

例子:

<button onclick="alert('Do this'+' and that')">Press This</button>

回答by klidifia

Hmm but how about for something like this:

嗯,但是对于这样的事情呢:

<a href="#" onclick="if (a_function_to_get_language() == 'en') {
    alert('Some message');
}
else {
    alert('Some message in a different language');
}">Action</a>

回答by user7850612

  • Alert on nav page
  • 导航页面上的警报
  •            <script>
                  function myFunction() {
                alert("You Are Not Signed In!");
                       }
                     </script>