Home  >  Q&A  >  body text

javascript - JS function receives parameters and pops up

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>函数接收参数并弹出</title>
    <style type="text/css">
        body {font: 12px/1.5 Tahoma; text-align: center;}
        input {border: 1px solid #ccc; padding: 3px;}
        button {cursor: pointer;}
    </style>

    <script type="text/javascript">
    var myFn = function(a,b) {
        alert(a.value);
        alert(b.value)
        
    };
        window.onload = function() {
            var oInput = document.getElementsByTagName("input");
            var oBtn = document.getElementsByTagName("button")[0];
            oBtn.onclick = function() {
                myFn(oInput[0],oInput[1])
            }
        };
    </script>

</head>
<body>
<p><input type="text" value="北京市"></p>
<p><input type="text" value="朝阳区"></p>
<p><button>传参</button></p>
</body>
</html>
  1. Can I alert pop out the content in the box at once?

怪我咯怪我咯2663 days ago662

reply all(2)I'll reply

  • 阿神

    阿神2017-07-05 10:54:48

    Can’t. One sentence of alert() pops up the alert box once.

    After closing an alert box, the next alert box will pop up.

    You can concatenate two input values ​​into a string, for example:

    var myFn = function(a,b) {
            var str = a.value + ',' +  b.value;
            alert(str);        
        };
    

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 10:54:48

    Can’t.

    The pop-up window is executed synchronously. When alert(a.value); is called, the system will no longer execute (blocked). The pop-up window must be closed before the following code can continue to execute.

    So, the solution is not to use the alert function, but to use a third-party pop-up window, which can pop up multiple ones at the same time.

    reply
    0
  • Cancelreply