简体   繁体   English

需要点击两次才能提交表单 - JQuery(如 stackoverflow 审核问题)

[英]Require two clicks to submit a form - JQuery (like stackoverflow review question)

I am trying to replicate the review post feature you see here on stackoverflow, I am struggling with the requiring two clicks to submit portion of this though.我正在尝试复制您在 stackoverflow 上看到的评论帖子功能,但我正在努力解决需要单击两次才能提交部分内容的问题。 My searches have returned multiple problems with validate but nothing quite pertaining to what I am looking for.我的搜索返回了验证的多个问题,但与我正在寻找的内容无关。

I have a standard form:我有一个标准表格:

<form action="http://www.google.com/">
  <textarea name="text"></textarea>
  <input type="submit" name="review" value="Review" />
</form>

With the following jQuery:使用以下 jQuery:

$(function(){
  $('form').submit(function(){
    $('html, body').animate({scrollTop : $(".Anchor").offset().top}, 300);
    return false; // to cancel form action
  });
});

How can I make it so when the submit is pressed it goes to the anchor, then the next time it is pressed it submits the form?我怎样才能使它在按下提交时转到锚点,然后在下一次按下时提交表单? I would like to change the value from review to submit also, but I'm sure I can figure that part out after receiving a reply to the initial question.我也想将值从 review 更改为 submit,但我相信在收到对初始问题的回复后,我可以弄清楚那部分。

A live sample of what I have can be found here on CodePen可以在 CodePen上找到我所拥有的实时样本

By Using a variable within your function, you can toggle the variable from true to false upon click.通过在函数中使用变量,您可以在单击时将变量从true切换为false With this method you can manipulate anything you want the second action to be within an else statement.使用此方法,您可以操作任何您希望第二个操作位于 else 语句中的内容。

An example of this based on the code you provided:基于您提供的代码的示例:

$(function(){
  let hasUserReviewedTheForm = false;
  $('form').submit(function(){
    if(!hasUserReviewedTheForm) {
        $('html, body').animate({scrollTop : $(".Anchor").offset().top}, 300);
        //change the form name and change the variable value
        hasUserReviewedTheForm = true;
        return false; // to cancel form action
    }
    else {
      //Submit the form
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM