【问题标题】:Jquery Confirmation Does Not AppearJquery 确认不出现
【发布时间】:2013-02-28 16:19:25
【问题描述】:

首先我想说我对 Jquery / 客户端脚本非常陌生。当我的老板想要某种方式让客户确认提交的表单时,我有点盲目。

这是我的 jquery / 标题:

<head>
    <title>Access Point</title>
    <meta http-equiv="Content-Type" content="text/html"; charset="utf-8" />
    <link rel="stylesheet" href="<?php echo base_url();?>css/mainstyle.css" type="text/css" />
    <link rel="stylesheet" href="<?php echo base_url();?>css/secondarystyles.css" type="text/css"/>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        $("#signinform").submit( function(e)
        {
            if (!confirm("If you click OK you will be inserted into student queue. Please take a seat and wait."))
            {
                e.preventDefault();
                return;
            }
        });
    </script>
</head>

这是我的表格:

<?php echo form_open('staff_controller/agree', 'id="signinform"') ?>
            <input type="checkbox" id="agree" name="options" value="agree"<?php echo form_checkbox('options','agree') ?>I have read and understood the above <br /> <font color="#ff0000" size="3">(Please click on the box)</font></input>
            <br />
            <br />
            <br />
            <?php
                echo form_submit('submit', 'Submit'); 
                echo anchor('staff_controller/studentlogin', 'Cancel'); 
                echo form_close();
            ?>

我的 php 脚本检查复选框是否被选中(以同意我们的要求)并检查是否单击了提交。如果单击它,则将值提交到数据库中。据我了解,我可以继续使用这种风格来处理我的数据,我只希望 jquery 在中间,让用户知道他们正在提交。我在互联网上找到了这段代码,但我不知道如何调试它。我还计划一个 jquery 确认来检查你是否要“取消”表单提交

编辑 1:

查看它“应该”工作的源代码:

表格 action="https://www.finaidtest.com/index.php/staff_controller/agree" id="signinform" method="post" accept-charset="utf-8"

然后是我更新的确认:

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<script>
    $(document).on('submit', "#signinform", function(e)
    {
        if (!confirm("If you click OK you will be inserted into student queue. Please take a seat and wait"))
        {
            e.preventDefault();
            return;
        }
    });
</script>

编辑 2

感谢 Musa,现在一切正常!谢谢!!

代码:

<script src="<?php echo base_url();?>javascript/js/jquery.js" type="text/javascript"></script>
    <script>
        $(document).on('submit', "#signinform", function(e)
        {
            if (!confirm("If you click OK you will be inserted into student queue. Please take a seat and wait"))
            {
                e.preventDefault();
                return;
            }
        });
    </script>

【问题讨论】:

    标签: php jquery forms popup confirmation


    【解决方案1】:

    您必须等待元素被创建,然后才能将事件处理程序绑定到它。使用 $(document).ready 确保在设置处理程序之前创建您的元素。

    $(document).ready(function(){
        $("#signinform").submit( function(e)
        {
            if (!confirm("If you click OK you will be inserted into student queue. Please take a seat and wait."))
            {
                e.preventDefault();
                return;
            }
        });
    });
    

    你也可以使用委托来附加事件,这样你就不必等待 dom 被加载

        $(document).on('submit', "#signinform", function(e){
            if (!confirm("If you click OK you will be inserted into student queue. Please take a seat and wait."))
            {
                e.preventDefault();
                return;
            }
        });
    

    【讨论】:

    • 我也更喜欢回报的表现力。在 if 子句中而不是 e.preventDefault();返回;你可以把return false。否则它应该默认返回true
    • 试过两个例子都没有弹出?
    • 它不能工作,因为我必须使用 Ajax/Jquery 来提交表单吗?我正在使用 post 到 php。我有点迷失于 Jquery / Javascript 的东西。
    • 刚刚修好了。 Jquery 的来源出了点问题。很奇怪。我刚刚下载了jquery,一切正常。谢谢你,先生。将更新原帖!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2023-04-08
    • 2011-03-20
    • 2014-05-06
    • 2011-04-17
    • 1970-01-01
    • 2016-06-16
    相关资源
    最近更新 更多