薛乎乎~ 2024-04-26 17:27 采纳率: 81%
浏览 0
已结题

(标签-jquery)

使用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>
</head>
<body>
    <!-- 取票系统界面设计 -->
    <form action="" method="post" id="f" onsubmit="return myFunction()">
        <div id="ticketingsystem">
            <div id="headerName">
                取票系统
            </div>
            <div id="inputTitle">
                请输入兑换码:
            </div>
            <div id="input">
                <input id="inputCode" type="text">
            </div>
            <div id="button">
                <input type="submit" onclick="qupiao()" value="取票">
                <input id="chongxin" type="button" onclick="chongxinshuru()" value="重新输入">
            </div>
        </div>
    </form>
</body>
<style>
    body {
        margin: 0px;
        padding: 50px;
    }
    #ticketingsystem {
        width: 450px;
        height: 150px;
        border: 2px solid rgb(83 145 253);
    }
    #headerName {
        height: 48px;
        background-color: rgb(183, 207, 249);
        border-bottom: 2px solid rgb(83 145 253);
        line-height: 48px;
        text-align: center;
        font-weight: 540;
        font-size: large;
    }
    #inputTitle {
        height: 48px;
        width: 178px;
        float: left;
        border-right: 2px solid rgb(83, 145, 253);
        border-bottom: 2px solid rgb(83 145 253);
        line-height: 48px;
        text-align: center;
        font-weight: 540;
        font-size: large;
    }
    #input {
        height: 48px;
        width: 270px;
        border-bottom: 2px solid rgb(83 145 253);
        float: left;
        line-height: 45px;
    }
    #inputCode {
        margin-left: 5px;
        height: 30px;
        width: 200px;
    }
    #button {
        height: 50px;
        line-height: 50px;
        text-align: center;
    }
</style>
<script>
    var realCode = "99632570063166";
    var inputCode = document.querySelector("#inputCode");
    function qupiao() {
        if (inputCode.value == realCode) {
            alert("取票成功!");
        } else if (inputCode.value == '') {
            alert("请输入兑换码!");
        } else if (inputCode.value != realCode) {
            alert("兑换码不正确!");
        }
    }
    function chongxinshuru() {
        inputCode.value = "";
        document.getElementById("inputCode").focus();
    }
</script>
</html>

  • 写回答

6条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-04-26 17:27
    关注

    引自免费微信小程序:皆我百晓生

    为了更好地帮助您解决这个问题,我已经仔细阅读了您的代码,并按照您的要求进行了相应的修改。以下是修改后的HTML、CSS和JavaScript代码。

    首先,我们将使用jQuery来增强功能。确保在页面头部引入jQuery库(例如通过CDN):

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    

    接下来是修改过的HTML部分,主要是在提交按钮上添加了一个id,以便于我们使用jQuery进行事件处理:

    <!-- ... -->
    <form action="" method="post" id="f">
        <!-- ... -->
        <div id="button">
            <input type="button" id="submitBtn" value="取票">
            <input id="chongxin" type="button" onclick="chongxinshuru()" value="重新输入">
        </div>
    </form>
    <!-- ... -->
    

    然后是CSS部分,保持不变:

    /* ... */
    

    最后是JavaScript部分,将原有的onsubmit事件处理函数移除,改用jQuery处理表单提交,同时优化了一些代码结构:

    $(document).ready(function () {
        var realCode = "99632570063166";
        var $inputCode = $("#inputCode");
    
        function checkAndSubmit() {
            var inputValue = $inputCode.val().trim();
    
            if (inputValue === "") {
                alert("请输入兑换码!");
            } else if (inputValue !== realCode) {
                alert("兑换码不正确!");
            } else {
                alert("取票成功!");
            }
    
            // 清空输入框并获取焦点
            $inputCode.val("").focus();
        }
    
        // 绑定提交按钮点击事件
        $("#submitBtn").click(checkAndSubmit);
    
        // 保留重新输入按钮的原有功能
        $("#chongxin").click(chongxinshuru);
    });
    

    这样我们就使用jQuery实现了取票系统的功能。当用户点击“取票”按钮时,会检查输入的兑换码是否有效,然后根据结果显示相应提示。同时,“重新输入”按钮的功能保持不变。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 9月13日
  • 已采纳回答 9月5日
  • 创建了问题 4月26日