两个jQuery函数监听同一个按钮,但只有一个可以工作 [英] two jQuery functions listening to the same button click but only one works

查看:80
本文介绍了两个jQuery函数监听同一个按钮,但只有一个可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个函数的代码(记住它的测试过程并且工作正常):

  $('。create- ('click',function()
{

grab_invoice_data();
//声明一个变量
var jsonObj = invoice_data;

//让我们转换我们的JSON对象
var postData = JSON.stringify(jsonObj);

//让我们将字符串化的json放入一个变量中,用于发布
var postArray = {json:postData};

//如果cookie存在
var i_n = $('。inv-number')。val();
$。如果($ .cookie('ic')){
var ck_inv_created =($($'$'$'$'$) .cookie('ic'));
。.cookie('ic',(++ ck_inv_created));
} else {
$ .cookie('ic',++ inv_created );
}
})

第二个函数的代码完美,这是新增的功能):

()函数(){
$ .get(。code $(document).ready(function(){
$(。reloadpg http://localhost/einvoice/inum.php,函数(data){
$(。on input).val(data);
});
});
});

问题是,当我在代码中放置第二个函数时它工作,但第一个函数从加工!记住,他们听同样的按钮..所以变得不同,并试图做到这一点:

  $('。create ('click',function()
{

grab_invoice_data();
//声明一个变量
var jsonObj = invoice_data;

//让我们转换我们的JSON对象
var postData = JSON.stringify(jsonObj);

//让我们将字符串化的json放入变量中以发布
var postArray = {json:postData};
//下面是我从第二个函数中添加的代码,第一个函数不在这里工作..当它独自在这个函数之外时,它会工作,但它赢得了'在这里

$ .download(php / json.php,postArray,'post');
$ .get(/einvoice/inum.php,function(data) {
$('。inv-number')。val(data);
});
//以上是我从第二个函数中添加的代码..
/ /下面是该函数的其余部分代码轧,请记住只有下面的代码是bing执行上述不是..它不刷新页面的一部分它应该调用PHP脚本成功tho,但我将不得不刷新页面我自己看到的数字更新

//如果cookie存在
var i_n = $('。inv-number')。val();
$ .cookie('lid',++ i_n,{expires:365});
//发票创建
if($ .cookie('ic')){
var ck_inv_created =($ .cookie('ic'));
$ .cookie('ic',(++ ck_inv_created));
} else {
$ .cookie('ic',++ inv_created);

))

我需要这两个函数才能成功工作点击不只一个!任何帮助将不胜感激

解决方案

正如评论中所讨论的那样,问题在于您意外插入了一条并不意味着在那里:

  $。download(php / json.php,postArray,'post'); 

只要删除这一行,它应该都可以工作。


Code for the first function(keeping in mind its tested and it works fine):

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

Code for the second function(it works perfectly. this is the newly added function):

$(document).ready(function(){
$(".reloadpg").click(function(){
    $.get("http://localhost/einvoice/inum.php", function(data) {
        $(".on input").val(data);
    });  
});
});

The problem is that when i placed the second function in my code it worked but the first function stopped from working! keeping in mind they listen to the same button.. so get disparate and tried to do this code:

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

i need both function to work successfully when the button is clicked not just one! any help would be appreciated

解决方案

As discussed in the comments, the problem is that you accidentally inserted a line which was not meant to be there:

$.download("php/json.php", postArray, 'post');

Just remove this line and it should all work.

这篇关于两个jQuery函数监听同一个按钮,但只有一个可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆