Php Can';t使用jQuery通过AJAX调用获取元素的值

Php Can';t使用jQuery通过AJAX调用获取元素的值,php,jquery,Php,Jquery,我有一些jQuery代码如下: // Called right away after someone clicks on the vote up link $('.vote_up').click(function() { var problem_id = $(this).attr("data-problem_id"); var class_name = ".votes_"+problem_id; alert ("class name: " + clas

我有一些jQuery代码如下:

// Called right away after someone clicks on the vote up link
$('.vote_up').click(function() 
{        
    var problem_id = $(this).attr("data-problem_id");

    var class_name = ".votes_"+problem_id;
    alert ("class name: " + class_name);

    var x = $(".votes_214").val();
    alert ("x: " + x);      // returns nothing at first, but correct values later.

    vote(problem_id , 1);

    //Return false to prevent page navigation
    return false;       
});
这是投票函数:

// Global function
var vote = function(problem_id , vote) 
{
    var dataString = 'problem_id=' + problem_id + '&vote=' + vote;

    // The person is actually logged in so lets have him vote
    $.ajax({
                type: "POST",
                url: "/problems/vote.php",
                data: dataString,
                success: function(html)
                {   
                    alert("data: " + html);
                    var class_name = ".votes_"+problem_id;
                    alert ("class name: " + class_name);

                    var x = $(class_name).val();

                    alert ("curr val: " + x);
                    $(class_name).val(html);
                },
                error : function(html) 
                {
                    errorMessage = html;

                    if ( html == "not_logged_in" )
                    {
                        queue.login = false;

                        //set the current problem id to the one within the dialog
                        $problemId.val(problem_id);                 

                        // Try to create the popup that asks user to log in.
                        //  $dialog.dialog('open');
                        $("#loginpopup").dialog();

                        errorMessage = "";

                        // prevent the default action, e.g., following a link
                        return false;
                    }
                    else
                    if ( errorMessage == "already_voted" )
                    {
                        // Display a dialog box saying that the user already voted
                        $('<div />').html('You already voted this way on this problem.').dialog();
                    }
                    else
                    if (  errorMessage == "error_getting_vote" )
                    {

                        $('<div />').html('Error getting existing votes.').dialog();
                    }
                    else
                    {
                        // ? :)
                    }    
                } // End of error case  
            }); // Closing AJAX call.
return false;
    };    
//全局函数
var投票=函数(问题编号,投票)
{
var dataString='problem_id='+problem_id+'&vote='+vote;
//此人实际上已登录,所以让他投票
$.ajax({
类型:“POST”,
url:“/problems/vote.php”,
数据:dataString,
成功:函数(html)
{   
警报(“数据:+html”);
var class_name=“.vows_”+问题id;
警报(“类名:”+类名);
var x=$(class_name).val();
警报(“当前值:+x”);
$(类名).val(html);
},
错误:函数(html)
{
errorMessage=html;
如果(html==“未登录”)
{
queue.login=false;
//将当前问题id设置为对话框中的问题id
$problemId.val(问题id);
//尝试创建请求用户登录的弹出窗口。
//$dialog.dialog('open');
$(“#loginpopup”).dialog();
errorMessage=“”;
//防止默认操作,例如,跟踪链接
返回false;
}
其他的
如果(errorMessage==“已投票”)
{
//显示一个对话框,说明用户已投票
$('').html('您已经在这个问题上投票了。')。dialog();
}
其他的
如果(errorMessage==“获取投票时出错”)
{
$('').html('获取现有投票时出错。').dialog();
}
其他的
{
// ? :)
}    
}//错误案例结束
});//结束AJAX调用。
返回false;
};    
进行AJAX调用并返回正确的值。问题是,我无法获取并设置我在PHP中设置为问题id的任何实际值的类投票权\u problem\u id\u值:

        echo '<span class="half_text" style="color: #B77F37;">'.$problem_date.'</span> <span id="votes" class="votes_'.$problem_id.' half_text" style="padding-left: 10px;">'.$vote.'</span><strong> <a class="vote_up" style="font-size: 80.0%; color: #295B7B; font-weight:bold; text-decoration:none;" href="#" data-problem_id="'.$problem_id.'">Important</a></strong> | <strong><a class="vote_down" style="font-size: 80.0%; color: #295B7B; font-weight:bold; text-decoration:none;" href="#" data-problem_id="'.$problem_id.'">Not Important</a></strong>';
echo'.$problem\u date'.$vote'.>;
知道我做错了什么吗?为什么我无法获取span class=“投票数”.$problem\u id.“半文本”的值或设置其内容

谢谢

.val()
用于表单元素,而不是span。使用
.text()
(或者可能使用
.html()
)获取span中包含的文本

var x = $(class_name).text();
或者,要在span上设置自定义属性,请使用
.attr()

或者,要将自定义数据与范围关联,请使用
.data()

.val()
用于表单元素,而不是span。使用
.text()
(或者可能使用
.html()
)获取span中包含的文本

var x = $(class_name).text();
或者,要在span上设置自定义属性,请使用
.attr()

或者,要将自定义数据与范围关联,请使用
.data()


我看到您的PHP将POST值视为全局值。如果您的register\u globals设置为off,$vote和$problem\u id将被取消设置,除非您明确设置它们。最好的做法是关闭register\u globals,改用superglobal$\u POST

因此,请尝试使用
class=“投票”$\u POST['problem\u id']。“half\u text”

下面是关于register_globals的PHP文档:


我看到您的PHP将POST值视为全局值。如果您的register\u globals设置为off,$vote和$problem\u id将被取消设置,除非您明确设置它们。最好的做法是关闭register\u globals,改用superglobal$\u POST

因此,请尝试使用
class=“投票”$\u POST['problem\u id']。“half\u text”

下面是关于register_globals的PHP文档:


只是尝试使用.text()执行此操作,但仍然无效。还有什么可能出错的吗?只是试着用.text()来做,但仍然不起作用。还有什么可能出错的吗?$(类名)是数组吗?does$(class_name)[0].val(html);work?@sissonb-
$(类名)
是一个jQuery对象,而不是数组
$(class_name)[0]
是元素或
未定义的
,两者都没有
.val()
方法。所以,不,那不行。$(类名)是数组吗?does$(class_name)[0].val(html);work?@sissonb-
$(类名)
是一个jQuery对象,而不是数组
$(class_name)[0]
是元素或
未定义的
,两者都没有
.val()
方法。所以,不,那不可能。你是如何得出“全球注册”存在任何问题的结论的?OP只显示了部分PHP代码,负责生成部分HTML。我给出-1您是如何得出“全球注册”存在任何问题的结论的?OP只显示了部分PHP代码,负责生成部分HTML。我给-1
var x = $(class_name).data("value");
...
$(class_name).data("value", html);