2017-04-10 112 views
1

我有一個問題。我想當我點擊一個h1標籤,然後h1標籤id添加輸入值並自動提交。但自動提交不起作用。 如果我點擊提交按鈕,然後提交其數據。jquery自動提交不工作

<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(); 
}); 

回答

1

的問題是在這裏:

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

是什麼path這裏?

你必須使用ID,類,屬性的名稱作爲selector,但在你的情況下,在html中沒有path。因此請將path更改爲:

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

並重試。

+0

對不起了我的錯誤,但仍然沒有工作自動提交 – Sakib

0

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

+0

對不起了我的錯誤,但仍然沒有工作自動提交 – Sakib

1

您可以使用一個簡單的方法爲您的按鈕類型是submit。給它分配一個id,然後在文檔上單擊編程單擊它。

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

的JS:

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

,如果你想提交表單無論是事件,您可以始終遵循上述辦法。

0

在你的JQuery你提到你提到path但什麼是

只是單純地改變成h1 tag ID,以便變化如下

的$(document)。在(「點擊」,' #我-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>

+0

對不起了我的錯誤,但仍然沒有工作自動提交。 – Sakib

+0

你想要什麼'自動提交的手段' – Abi

+0

當我點擊h1時,這個h1標籤id輸入到輸入值並自動從Submited。我想通過一次點擊完成所有作品 – Sakib