【问题标题】:Jquery auto submit not workingJquery自动提交不起作用
【发布时间】:2017-04-10 19:18:04
【问题描述】:

我有问题。我想要当我点击一个 h1 标签时,然后 h1 标签 ID 添加输入值并自动提交。但是自动提交不起作用。 如果我点击提交按钮,那么它的提交数据。

   <h1 id="my-id">sadasdsa</h1>
<form action="" method="POST" id="aweberform">
    <p><input type="text"  name="countrycode"  value="" id="country"></p>
    <button type="submit"  name="submit"  class="btn">Submit</button>
</form>


jQuery:

$(document).on('click', 'h1', function () {
    //alert(this.id);
    $("h1").text(this.id);
    $("input").val(this.id);
    $("#aweberform").submit();       
});
$(document).click(function(){
    $("#aweberform").submit();
});

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    问题出在这里:

    $(document).on('click', 'path', function () {
    

    这里的path 是什么?

    您必须使用属性的 id、类、名称作为 selector,但在您的情况下,html 中没有 path。所以把path改成:

    $(document).on('click', '#my-id', function () {
    

    然后再试一次。

    【讨论】:

    • 对不起,这是我的错误,但仍然无法自动提交
    【解决方案2】:

    您可以使用更简单的方法,因为您的按钮类型是submit。为其分配一个 id,然后在文档上单击以编程方式单击它。

    <button type="submit"  name="submit"  class="btn" id="submitBTN">Submit</button>
    

    JS:

    $(document).click(function(){
        $("#submitBTN").click();
    });
    

    如果您想提交表单,无论是什么事件,您都可以按照上述方法。

    【讨论】:

      【解决方案3】:

      只需更新#my-id 而不是路径:

      $(document).on('click', '#my-id', function () {
          //alert(this.id);
          $("h1").text(this.id);
          $("input").val(this.id);
          $("#aweberform").submit();       
      });
      $(document).click(function(){
          $("#aweberform").submit();
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <h1 id="my-id">sadasdsa</h1>
      <form action="" method="POST" id="aweberform">
          <p><input type="text"  name="countrycode"  value="" id="country"></p>
          <button type="submit"  name="submit"  class="btn">Submit</button>
      </form>

      【讨论】:

      • 对不起,这是我的错误,但仍然无法自动提交
      【解决方案4】:

      在你的 JQuery 中你提到你提到 path 但那是什么

      只需将其更改为h1 tag的id即可,更改如下

      $(document).on('click', '#my-id', function () {

      运行示例

      $(document).on('click', '#my-id', function () {
          //alert(this.id);
          $("h1").text(this.id);
          $("input").val(this.id);
          $("#aweberform").submit();       
      });
      $(document).click(function(){
          $("#aweberform").submit();
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <h1 id="my-id">sadasdsa</h1>
      <form action="" method="POST" id="aweberform">
          <p><input type="text"  name="countrycode"  value="" id="country"></p>
          <button type="submit"  name="submit"  class="btn">Submit</button>
      </form>

      【讨论】:

      • 对不起,这是我的错误,但仍然无法自动提交。
      • 你到底想要什么'自动提交意味着'
      • 当我点击 h1 时,这个 h1 标签 id 输入到输入值并自动提交。我想一键完成所有工作
      猜你喜欢
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多