How to get input via a popup and place text in a variable via javascript/jquery

前端 未结 2 1537
误落风尘
误落风尘 2021-02-08 20:49

I have a button on my page. When clicked a popup box should appear allowing the user to enter text. When OK/Submit is pressed, my jscript will then perform some functions usin

相关标签:
2条回答
  • 2021-02-08 21:02

    in it's simplest form, you could use prompt(question, default): (taken from w3schools: http://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt)

    function myFunction(){
        var x;
        var name=prompt("Please enter your name","Harry Potter");
        if (name!=null){
           x="Hello " + name + "! How are you today?";
          alert(x);
       }
    }
    

    anything else would require lots of javascript & CSS to create layers with buttons and click events on those buttons

    0 讨论(0)
  • 2021-02-08 21:20

    Paste this code inside your head tags between script tags

    HTML

    <button id="button">Get Text</button>​
    

    JS

    window.onload=function()
    {
        var el=document.getElementById('button');
        el.onclick=function(){
            var my_text=prompt('Enter text here');
            if(my_text) alert(my_text); // for example I've made an alert
        }
    }
    

    DEMO.

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