另一个Ajax函数触发的Ajax函数不起作用

Ajax function triggered from another ajax function not working

本文关键字:函数 Ajax 不起作用 另一个      更新时间:2023-09-26

我正在编写一个脚本,该脚本从一个表中复制一个行ID,并在页面上创建一个新表,其中包含通过复选框选择的所有行,以便创建所选结果的比较表。我遇到了两个ajax调用之间的协同问题。

当在原始表中创建以下行时,分配给该行的<a>元素的类会触发一个ajax调用,然后该调用会填充一个显示在页面上的带有附加信息的模态。

这是线路

echo "<td><a id='$id' data-toggle='modal' href='#provmodal' class='push'>".$results['provider_name']."</a>";

当模态以这种方式触发时,信息填充得很好。

现在,在填充新表的脚本中,我调用另一个单独的脚本,该脚本会重新查询所选的行id并设置html。

这是脚本的一部分:

 $('.compareCheck:checked').each(function(){
            var ele_id = $(this).attr('id');
            $.ajax({
            type : 'post',
            url : 'compare.php', //query 
            data :  'post_id='+ ele_id, // passing id via ajax
            dataType: "json",
            success : function(data){
                var row = "<tr class='removeRow'>";
                row += "<td>" + data.provider_num + "</td>";
//HERE IS WHERE THE RE-CREATION OF THE MODAL CALL GOES '/
                row += "<td><a id='" + ele_id + "' data-toggle='modal' href='#provmodal' class='push'>" + data.provider_name + "</a></td>";
                row += "<td style='text-align:right;'>$" + formatNumber(data['233_net_charity_care']) + "</td>";
                row += "<td style='text-align:right;'>$" + formatNumber(data['291_cost_of_non_mcr_bad_debts']) + "</td>";
                row += "<td style='text-align:right;'>$" + formatNumber(data['301_cost_of_uncomp_care']) + "</td>";
                row += "<td style='text-align:right;'>" + ((data['233_net_charity_care']/data['301_cost_of_uncomp_care'])*100).toFixed(1) + "%</td>";
                row += "<td style='text-align:right;'>" + ((data['291_cost_of_non_mcr_bad_debts']/data['301_cost_of_uncomp_care'])*100).toFixed(1) + "%</td>";
                row += "</tr>";
                $("#compareTable > tbody").append(row);
              }
            });
              });

正如您在我当前的实现中看到的,我使用的是ele_idvar,但我也尝试过data.iddata['id']之类的东西。所有这些都会触发模态,但不会从php脚本中产生任何结果。

以下是我的两个php脚本:

脚本A:填充模态-(modalquery.php)

<?php
require_once("link_costreport_2013.php");
$id = $_POST['post_id'];
$modalquery = $link->prepare("SELECT * FROM s10 WHERE id = :id");
$modalquery->bindParam(':id', $id, PDO::PARAM_INT);
$modalquery->execute();
$modalresults = $modalquery->fetch();
print_r("<h4>State: ".$modalresults['state']."</h4>");
print_r("<h4>City: ".$modalresults['city']."</h4>");
print_r("<h4>Street: ".$modalresults['street']."</h4>");
print_r("<h4>Zip: ".$modalresults['zip']."</h4>");
print_r("<h4>County: ".$modalresults['county']."</h4>");
?>

脚本B-将重新查询转换为新比较表(compare.php)值的脚本

<?php
include_once('functions.php');
include_once('link_costreport_2013.php');
if(isset($_POST['post_id'])){
    $id = $_POST['post_id'];
}
    $query = $link->prepare("SELECT * 
                FROM `s10`
                            WHERE `id` = :id");
    $query->bindParam(':id', $id, PDO::PARAM_INT);
    $query->execute();
    $results = $query->fetch(PDO::FETCH_ASSOC);
    echo json_encode($results);
?>

如果有帮助的话,下面是我的脚本,用于将.push类转换为返回模态内容的ajax调用的触发器。

$(function(){
            $('.push').click(function(){
            var ele_id = $(this).attr('id');
            $.ajax({
            type : 'post',
             url : 'modalquery.php', // in here you should put your query 
            data :  'post_id='+ ele_id, // here you pass your id via ajax .
                   // in php you should use $_POST['post_id'] to get this value 
             success : function(r)
             {
                // now you can show output in your modal 
              $("#provmodal .modal-body").html(r).promise().done(function(){
              $("#provmodal").modal('show');  
                           });
             }
               });
              });
          });

我是以这种方式使用ajax和jquery的新手,所以任何见解都是非常棒的。

提前感谢

:EDIT:这是ID=1 时json_encode($results)的输出

{"id":"1","report_record_num":"548598","provider_num":"381301","provider_name":"COTTAGE GROVE COMMUNITY HOSPITAL","street":"1515 VILLAGE DRIVE","city":"COTTAGE GROVE","county":"LANE","state":"OR","zip":"97424-9700","cbsa":"21660","urban_or_rural":"Rural","ownership_type":"Voluntary, Nonprofit, Church","divider":"","divider2":"","1_cost_to_charge_ratio":"0.703459","2_net_rev_from_mcd":"3920096","3_recieve_sup_mcd_payments":"Y","4_include_if_yes":"N","5_dsh_or_sup_payments":"84890","6_medicaid_charges":"6192717","7_medicaid_cost":"4356323","8_dif_net_rev_and_cost":"351337","9_schip_net_rev":"0","10_stnd_alone_schip_charges":"0","11_stnd_alone_schip_cost":"0","12_diff_schip_rev_and_cost":"0","13_net_rev_from_state_local":"0","14_charge_under_state_law":"0","15_state_local_program_cost":"0","16_dif_between_net_rev_and_cost":"0","17_private_grants_and_donations":"6886","18_gov_grants":"0","19_tot_unreim_cost_mcd_schip_gov":"351337","201_tot_init_charity_for_uninsured":"593922","202_tot_init_charity_for_insured":"1072203","203_tot_init_charity_all":"1666125","211_cost_of_init_charity":"417800","212_cost_of_init_charity":"754251","213_cost_of_init_charity":"1172051","221_partial_pmt_charity_pat":"4385","222_partial_pmt_charity_pat":"8868","223_partial_pmt_charity_pat":"13253","231_net_charity_care":"413415","232_net_charity_care":"745383","233_net_charity_care":"1158798","241_charges_beyond_los_inc":"N","251_charges_beyond_los_lim":"0","261_total_bed_debts":"0","271_medicare_bad_debts":"79275","281_non_medicare_bad_debts":"-79275","291_cost_of_non_mcr_bad_debts":"-55767","301_cost_of_uncomp_care":"1103031","311_cost_of_unreim_and_uncomp":"1454368"}

:第二版:好的,所以我回去拍了一些正在发生的事情的照片。不知怎的,我的模态文本没有出现在第二个表<a class="push">元素中。以下是图片:

(https://i.stack.imgur.com/RaQss.jpg)-这在第一张表中!(https://i.stack.imgur.com/VLcJl.jpg)-当按下同一个链接时,第二秒会发生这种情况。我相信它不会触发ajax.push调用。

尝试更改:

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

用于:

$('.push').on('click', function(){

在第一种情况下,事件处理程序被添加到具有"push"类的每个元素中,该类在执行"click"函数时出现在DOM中。第二种情况是使用"on"函数的情况,它将事件处理程序添加到已经存在的元素和尚未在DOM中的元素中,这些元素是在执行adition后创建的,就像添加到表中的元素一样。

希望我能把自己说清楚,我正在努力提高我的英语。