【问题标题】:Disable button on click but still process form单击时禁用按钮但仍处理表单
【发布时间】:2023-03-14 05:15:01
【问题描述】:

我想在点击时禁用我的注册按钮以防止垃圾邮件,但我不想阻止按钮提交的表单提交。

我可以禁用点击按钮,但不知道为什么表单仍然无法提交...

按钮:

<?= form_open("course/registerCourse", "method='post'") ?>
   <input type="hidden" name="session" value="<?= $session["id"] ?>">
   <button type="submit" class="btn-sm btn btn-success register">REGISTER</button>
<?= form_close() ?>

jQuery:

$('.register').on('click',function() {
    $(this).prop("disabled",true);
    $("form").submit();
});

注册函数:

public function registerCourse()
{

    $user = $this->users_model->userAccountTP();
    $appParticipant = $user[0]["id"];

    $author_id = $this->session->userdata("ci_user_id");
    $appSession = $this->input->post('session');

    $this->ci_channel_entry_model->save_multiple(array(
        "channel_id" => 11,
        "title" => date("Y-m-d h:i:s"),
        "author_id" => $author_id,
        "status_id" => 7,
        "date" => date("Y-m-d h:i:s"),
        "data" => array(
            array(
                // application-session
                "field_id" => 28,
                "content" => $appSession
            ),
            array(
                // application-participant
                "field_id" => 29,
                "content" => $appParticipant
            )
        ),
        "hook" => FALSE
    ));

    redirect($this->agent->referrer());
}

【问题讨论】:

  • 你试过把 $("form").submit();在将 disabled 属性添加到按钮之前?
  • 这有什么意义?您的表格不必填写。垃圾邮件发送者可以直接将您的字段=值对直接提交到服务器,而无需加载您的表单。
  • 试过了还是不行
  • @MarcB 如果用户在提交表单时多次按下注册按钮,则会向服务器发布多个注册请求。禁用该按钮将允许表单仅提交一个请求。
  • 您能准确地显示您的表单代码吗?

标签: javascript php jquery html forms


【解决方案1】:

我认为您可能有错字,因为我看不到您在哪里声明 $form,所以也许这会有所帮助:

$('.register').on('click',function() {
    $(this).prop("disabled",true);
    $("form").submit();
});

【讨论】:

  • 谢谢 - 我已经解决了这个问题,但我仍然遇到问题。
【解决方案2】:

最终修改了我的表单以符合这个答案并且一切正常:

https://stackoverflow.com/a/5691065/4141833

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2020-07-23
    • 2013-03-05
    • 1970-01-01
    相关资源
    最近更新 更多