简体   繁体   English

使用jQuery无法通过AJAX调用获取元素的值

[英]Can't get value of an element by AJAX call by using jQuery

I have some jQuery code like this: 我有一些这样的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;       
});

and here is the vote function: 这是表决功能:

// 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;
    };    

The AJAX call gets made and returns the correct values. 进行AJAX调用并返回正确的值。 The problem is that I can not get and set the value of the class votes_problem_id_value that I set to any real value of the problem id here in my PHP: 问题是我无法在PHP中获取和设置我设置为问题ID的任何实际值的投票/问题_值类的值:

        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>';

Any idea what I am doing wrong and how come I can not get the value or set the contents of that span class="votes_'.$problem_id.' 知道我在做什么错了,为什么我无法获得该值或设置该span class =“ votes _'。$ problem_id的内容。 half_text" half_text”

Thanks!! 谢谢!!

.val() is for form elements, not spans. .val()适用于表单元素,而不适用于跨度。 Use .text() (or maybe .html() ) to get the text contained in a span. 使用.text() (或也许.html() )来获取跨度中包含的文本。

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

Or, to set a custom attribute on a span, use .attr() : 或者,要在跨度上设置自定义属性,请使用.attr()

var x = $(class_name).attr("value");
...
$(class_name).attr("value", html);

Or, to associate custom data with a span, use .data() : 或者,要将自定义数据与范围关联,请使用.data()

var x = $(class_name).data("value");
...
$(class_name).data("value", html);

I see that your PHP is treating POST values like they are globals. 我看到您的PHP将POST值视为全局值。 If your register_globals setting is off, $vote and $problem_id will be unset, unless you explicitely set them. 如果您的register_globals设置关闭,则除非明确设置它们,否则将不设置$ vote和$ problem_id。 The best practice is to leave register_globals off, and use the superglobal $_POST instead. 最佳实践是关闭register_globals,而使用超全局$ _POST。

So, try class="votes_'.$_POST['problem_id'].' half_text" 因此,请尝试class="votes_'.$_POST['problem_id'].' half_text" class="votes_'.$_POST['problem_id'].' half_text" instead. class="votes_'.$_POST['problem_id'].' half_text"代替。

Here's the PHP doc on register_globals: 这是register_globals上的PHP文档:

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

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