首页 > 解决方案 > 动态单选按钮不会调用第二个 JavaScript 函数

问题描述

我根据带有 name 的下拉列表动态创建单选按钮afl_round。这可以无缝地工作:

<script type="text/javascript">
jQuery(document).ready( function($) {
    var valueCheck;
    jQuery('#afl_round').on( 'change', function () {
         afl_round = $('#afl_round').val();
     jQuery.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",
        data: {
            action: 'call_slate_radio_buttons_ownership',
            afl_round: afl_round,
        },
         success:function(output){
             jQuery('#radio_buttons').html( output );
         }
     });
    }).change();
});
</script>

然后,我希望能够#ownership_table根据单选按钮的选择进行更改。但是,我发现这个脚本似乎根本没有运行。

当我直接通过而不是 javascript 生成单选按钮时php,该表与下面的代码完美配合,所以我觉得它一定与 javascript 的级联性质有关?

这些脚本是按顺序排列的,因为它们是在网页中编写的。

<script type="text/javascript">
jQuery(document).ready( function($) {
   jQuery('input:radio[name=afl_slate]').on( 'change', function () {
         afl_slate = $('input:radio[name=afl_slate]:checked').val();
     jQuery.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",
       data: {
            action: 'call_slate_ownership_table',
            afl_slate: afl_slate,
        },
         success:function(output){
             jQuery('#ownership_table').html( output );
         }
     });
    }).click();
});
</script>

我会很感激人们的任何意见,因为我确信这是我忽略的相当简单的事情。

提前致谢。


根据评论中的要求,我复制了我的 php 文件代码的简化版本来提供帮助。如上所述,第一个代码成功生成了单选按钮。第二个代码不会生成任何东西.....除非我在 html 中对单选按钮进行硬编码。所以我不相信这两个 php 文件有什么问题。我的想法是javascripts之间的链接不存在。

function get_slate_radio_buttons_ownership(){
global $wpdb;

$round = $_POST['afl_round'];
$myQuery = $wpdb->get_results('SELECT ds_slate_name, ds_slate_id FROM afl_master WHERE round = '.$round.' GROUP BY ds_slate_id');

if($myQuery){
$check = 0;
foreach ( $myQuery as $result ) 
    {
if($check == 0){$checked=' checked="checked"';} else{$checked='';}
echo'<input type="radio" name="afl_slate" value="'.$result->ds_slate_id.'"'.$checked.'/> '.$result->ds_slate_name.'<br>';
$check = $check + 1;
    }
}

echo'<br>';
wp_die();
}

add_action('wp_ajax_nopriv_call_slate_radio_buttons_ownership', 'get_slate_radio_buttons_ownership');
add_action('wp_ajax_call_slate_radio_buttons_ownership', 'get_slate_radio_buttons_ownership');

function get_slate_ownership_table(){
global $wpdb;

$slate = $_POST['afl_slate'];
$myQuery = $wpdb->get_results('SELECT * FROM afl_master WHERE ds_slate_id = "'.$slate.'" ORDER by ds_selected DESC');

echo '<div style="overflow-x:auto;">';
echo '<table>';
echo "<tr><th>Player Name</th><th>Value</th></tr>"; 

if($myQuery){
foreach ( $myQuery as $result ) 
{
$ds_value = number_format(ROUND(1000*$result->dreamTeamPoints/$result->ds_salary_hist,1),1);

    echo '<tr><td>'.$result->player_name.'</td><td>'.$ds_value.'x</td></tr>';
    }
}
echo '</table>';
echo '</div>';

wp_die();
}

add_action('wp_ajax_nopriv_call_slate_ownership_table', 'get_slate_ownership_table');
add_action('wp_ajax_call_slate_ownership_table', 'get_slate_ownership_table');

标签: javascriptphpajaxradio-button

解决方案


推荐阅读