[英]two jQuery functions listening to the same button click but only one works
第一個功能的代碼(請記住它已經過測試並且可以正常工作):
$('.create-invoice').on('click',function()
{
grab_invoice_data();
// Declare a variable
var jsonObj = invoice_data;
// Lets convert our JSON object
var postData = JSON.stringify(jsonObj);
// Lets put our stringified json into a variable for posting
var postArray = {json:postData};
//if cookie exists
var i_n = $('.inv-number').val();
$.cookie('lid', ++i_n, { expires: 365 } );
//invoices created
if( $.cookie('ic') ){
var ck_inv_created = ($.cookie('ic'));
$.cookie('ic', (++ck_inv_created));
} else {
$.cookie('ic', ++inv_created);
}
})
第二個功能的代碼(工作正常。這是新添加的功能):
$(document).ready(function(){
$(".reloadpg").click(function(){
$.get("http://localhost/einvoice/inum.php", function(data) {
$(".on input").val(data);
});
});
});
問題是,當我將第二個函數放在代碼中時,它可以工作,但第一個函數停止工作! 請記住,他們會聽同一個按鈕。.因此,請截然不同,並嘗試執行以下代碼:
$('.create-invoice').on('click',function()
{
grab_invoice_data();
// Declare a variable
var jsonObj = invoice_data;
// Lets convert our JSON object
var postData = JSON.stringify(jsonObj);
// Lets put our stringified json into a variable for posting
var postArray = {json:postData};
//Below is the code i added from the second function to the first its not working here.. when it was alone outside this function it will worked but it won't here
$.download("php/json.php", postArray, 'post');
$.get( "/einvoice/inum.php", function(data) {
$('.inv-number').val(data);
});
//Above is the code i added from the second function..
//below is the rest of the code for the function which was there from the beginning, keep in mind only the below code is bing executed the above is not.. its not refreshing the part of the page it should its calling the php script successfully tho, but i will have to refresh the page my self to see the number updated
//if cookie exists
var i_n = $('.inv-number').val();
$.cookie('lid', ++i_n, { expires: 365 } );
//invoices created
if( $.cookie('ic') ){
var ck_inv_created = ($.cookie('ic'));
$.cookie('ic', (++ck_inv_created));
} else {
$.cookie('ic', ++inv_created);
}
})
單擊按鈕時,我不僅需要兩個功能,而且還要使它成功運行! 任何幫助,將不勝感激
您可以嘗試從第一個調用第二個函數。
$('.create-invoice').click(function(){
secondFunction();
//rest of code
});
var secondFunction = function(){
//code
};
正如評論中所討論的那樣,問題在於您不小心插入了原本不應該存在的行:
$.download("php/json.php", postArray, 'post');
只需刪除此行,它應該可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.