Jquery 实现两个数相加

编写代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="jquery\jquery.js"></script>
</head>

<body>
    <div>
        number1:
        <input type="text" name="" id="number1">
        <br> 
        number2:
        <input type="text" name="" id="number2">
        <hr>
        <button id="res">result</button>
        <input type="text" name="" id="result">
    </div>

    <script>
        $(document).ready(function() {
            $("#res").click(function() {
                // let num1=0,num2=0;
                num1 = $("#number1").val();
                num2 = $("#number2").val();
                $("#result").val(Number(num1) + Number(num2));
                res = parseInt(num1) + parseInt(num2);
                // alert(res);

            })
        })
    </script>
</body>

</html>

运行结果

image

posted @ 2021-08-27 11:33  阿向向  阅读(916)  评论(0编辑  收藏  举报