更改选择框时JQuery会发出警报

时间:2014-05-25 20:10:38

标签: javascript php jquery

好的,所以我用同样的方式完成的所有其他功能到目前为止都有效。无论出于何种原因,这个都不会起作用。我不知道是否有一些我遗漏的东西,或者前面的代码中可能存在的错误可能导致这种情况。这是我的代码:

  jQuery("#teamTab_addPlayerForm").on('change', 'select#teamTab_suggestedPlayer', function(e) {
    e.preventDefault();
    alert('It Worked');

});

这是调用它的PHP文件:

 $output .= '<div id="ld_addPlayerFunction" style="clear:both; width:100%;"><h3>Add Player</h3>';
        $standins = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'kdc_user_sync ORDER BY computedMMR ASC');
        $output .= '<form id="teamTab_addPlayerForm" action="" method="POST">
            <select id="teamTab_suggestedPlayer" name="suggestedPlayer">
                <option value="base">Suggested Player</option>';
            foreach($standins as $standin){
                $playerID = $wpdb->get_var('SELECT ID FROM ' . $wpdb->prefix . 'leagueDesigner_players WHERE userID = ' . $standin->userID);
                $test = true;
                if($playerID != ''){
                    $leagueTest = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'leagueDesigner_league_players WHERE playerID = ' . $playerID);
                    foreach($leagueTest as $test){
                        if ($test->leagueID == $leagueID){
                            $test = false;
                        }
                    }
                }
                if($standin->computedMMR < ($suggestedMMR + 100) && $standin->computedMMR > ($suggestedMMR -100) && $test == true){
                    $output .= '<option value="' . $standin->userID . '">' . substr($standin->profileName, 0, 20) . ' - ' . $standin->computedMMR . ' MMR</option>';
                }
            } 

        $output .= '</select><br />';
        $output .= '</form>';

然后在脚本中稍后回显输出。所以一个用户告诉我使用文档而不是#teamTab_addPlayerForm并且它可以工作,但我想知道为什么这个以前的代码工作正常而且那个没有:

jQuery("#teamTab_teamListForm").on('change', 'select#teamTab_teamSelect', function (e) {
    e.preventDefault();
    jQuery(this).attr('disabled', true);
    var teamID = jQuery(this).val();
    jQuery('select#teamTab_leagueSelect').attr('disabled', true);
    var leagueID = jQuery('select#teamTab_leagueSelect').val();
    data = { action: "leagueDesignerTeamsTabLoadTeamPlayers", leagueID: leagueID, teamID: teamID };
        jQuery.ajax({
            type: 'POST', url: ajaxurl, data: data, dataType: 'html', success: function(response) {
                if (response == "error") {
                    jQuery('select#teamTab_teamSelect').attr('disabled', false);
                    alert('There was an error processing your request');
                }
                else {
                    jQuery('select#teamTab_teamSelect').attr('disabled', false);
                    jQuery('select#teamTab_leagueSelect').attr('disabled', false);
                    jQuery('.ld_teamEdit').remove();
                    jQuery('#ld_addPlayerFunction').remove();
                    jQuery('div#ld_teamTabTeamInfo').remove();
                    jQuery('#teamTab_teamListForm').after(response);
                }
                }});
});

这是以相同方式处理选择框的代码。这是PHP代码:

function leagueDesignerTeamsTabLoadLeagueTeams () { 
global $wpdb;
$leagueID = $_POST['leagueID']; //Pull the POST'd leagueID
$output = '<select id="teamTab_teamSelect" name="team"><option value="">Choose a Team</option>'; //Output...
$errors = 0; //Error checking...
$teams = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'leagueDesigner_teams WHERE leagueID = ' . $leagueID);
foreach($teams as $team){
    $output .= '<option value="' . $team->teamID . '"';
    if ($_POST['team'] == $team->teamID){
        $output .= ' selected';
    }
    $output .= '>' . $team->teamID . '. ' . $team->teamName . '</option>';
}
if (!$teams) {
    $errors++;
}
$output .= '</select>';
if ($errors == 0) { echo $output; } else { echo 'error'; }
die(); // this is required to return a proper result 

}

1 个答案:

答案 0 :(得分:0)

元素是在页面加载后创建的,因此您需要将其绑定到已存在的元素(参见下文)

$(document).on('change', '.class-name', function () {
    //Commands to run on change
});