繁体   English   中英

jQuery自动提交不起作用

[英]Jquery auto submit not working

我有个问题。 我想要当我单击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();
});

问题在这里:

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

这里的path是什么?

您必须使用id,class,属性名称作为selector ,但是在您的情况下,html中没有path 因此,将path更改为:

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

然后再试一次。

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

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

JS:

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

无论您要提交表单是什么事件,都可以始终采用上述方法。

只需更新#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> 

在您的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> 

暂无
暂无

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

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