Output disappeared Javascript simple innerHTML

后端 未结 3 1475
难免孤独
难免孤独 2021-01-13 03:01

I am new to javascript and on every simple thing i get some kind of problem but this seems un-solve-able to me. I googled and nothing simillar.

After i input data in

相关标签:
3条回答
  • 2021-01-13 03:31

    The <form> tag doesn't specify an action attribute, so when you click the submit button, the browser will submit the form to the current URL - which will look a lot like the page is refreshing.

    If you want to run the unesi function when the user clicks submit and prevent the HTML form from submitting, you need to change it slightly:

    <input type="submit" name="unos" value="Unesi i ispisi"
          onclick="unesi(); return false;">
    

    The return false prevents the form from submitting itself.

    0 讨论(0)
  • 2021-01-13 03:36

    Because the form submits and refreshes the page. Cancel the form request.

    <input type="submit" name="unos" value="Unesi i ispisi" onclick="return unesi()">
    

    and the function

    function unesi(){
        var userInput = document.getElementById('userInput').value;
        document.getElementById('ispis').innerHTML = userInput;
        return false;
    }   
    

    better option is to do it on the form level

    <form action="#" method="get" onsubmit="return unesi()">
    
    0 讨论(0)
  • 2021-01-13 03:37

    Instead of cancelling the form submitting, another option is to change

    <input type="submit" name="unos" value="Unesi i ispisi" onclick="unesi()">
    

    to

    <input type="button" name="unos" value="Unesi i ispisi" onclick="unesi()">
    

    This will make it so the form does not try to submit at all when the button is pressed.

    0 讨论(0)
提交回复
热议问题