Using “if/else” with OnClick

2019-02-18 02:15发布

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?

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.

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

5条回答
地球回转人心会变
2楼-- · 2019-02-18 02:35

  • Alert on nav page
  •            <script>
                  function myFunction() {
                alert("You Are Not Signed In!");
                       }
                     </script>
    
    查看更多
    ▲ chillily
    3楼-- · 2019-02-18 02:39

    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.

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

    查看更多
    Animai°情兽
    4楼-- · 2019-02-18 02:44

    Just DON'T put it in the onclick attribute.

    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>
    
    查看更多
    Bombasti
    5楼-- · 2019-02-18 02:58

    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>
    
    查看更多
    We Are One
    6楼-- · 2019-02-18 03:02

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

    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).

    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.

    Example:

    <button onclick="alert('Do this'+' and that')">Press This</button>
    
    查看更多
    登录 后发表回答