简体   繁体   English

jQuery将.each()函数放在.click()函数中

[英]jQuery placing .each() functions inside a .click() function

The .each() functions inside the .click() are not running. .each()内的功能.click()没有运行。 I do not know how to structure them to make it syntactically correct so jQuery will recognize and run them. 我不知道如何构造它们以使其在语法上正确,因此jQuery将识别并运行它们。

I tried tacking them on before the closing }); 我试图在结束之前解决这些问题}); but I either didn't do it right or that isn't how it's done. 但我要么做得不好,要么做得不好。 I tried Googling but other than my topic title I was at a loss for what to search. 我尝试了Google搜索,但是除了主题标题之外,我对搜索内容一无所知。 I did try jquery function inside function but that turned out to be a lost cause. 我确实尝试过在函数内部使用jquery函数,但事实证明这是一个丢失的原因。

EDIT: I have managed to get the first one to fire properly ( //POST specials ) however, the second one still isn't working. 编辑:我已经设法使第一个正确触发( //POST specials ),但是,第二个仍然无法正常工作。 I even tried to put the extras POST inside the .ajax() of the specials and it didn't work. 我甚至试图把extras POST里面.ajax()的的specials ,并没有奏效。

$('.class').find('#button').click(function() {

        //all kinds of variables here

        var dataString = {//variables:variables}
        console.log(dataString);

        $.ajax({
            type: "POST",
            url: "classes/reserve.php",
            data: dataString,
            cache: false,
            success: function(html)
            {
                //POST specials
                $('#specialscheck input:checked').each(function() {
                    var reservation = $('#reservation').val();
                    var special = parseInt($(this).attr('id'));
                    dataString = {reservation:reservation, special:special};
                    console.log(dataString);
                    $.ajax({
                        type: "POST",
                        url: "classes/insert_specials.php",
                        data: dataString,
                        cache: false,
                        success: function(html)
                        {
                            //$('.unitinfolist').html(html);
                        }
                    }); 
                });

                //POST extras
                $('#extrascheck input:checked').each(function() {
                    var reservation = $('#reservation').val();
                    var extra = parseInt($(this).attr('id'));
                    dataString = {reservation:reservation, extra:extra};
                    console.log(dataString);
                    $.ajax({
                        type: "POST",
                        url: "classes/insert_extras.php",
                        data: dataString,
                        cache: false,
                        success: function(html)
                        {
                            //$('.unitinfolist').html(html);
                        }
                    }); 
                });
            }
        });
    });

You should move the .each up into the success function of the jquery post, or set its async: false , to follow this pattern. 您应该将.each上移到jquery帖子的success函数中,或将其async: false设置为遵循此模式。

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType,
  async:false
});

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

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