#id孩子的$(这个)

时间:2011-12-24 03:32:20

标签: php javascript jquery selector

我为凌乱的脚本和标签道歉,我只是一个初学者。我想在div中使用类“reserveAPickupAppointmentLocation选择文本。这个div在$(this)里面。$(this)是许多中的选定条目。相关的行是:

var location = $(".reserveAPickupAppointmentLocation, this").text();

这将返回所有条目的位置,而不仅仅是$(this)。我如何只获得我想要的东西?

完整的jquery功能:

   $(".reserveAPickupAppointmentRoommateAppointment").click (function() {
    if ($(this).hasClass("confirm")) {
    }
    else {
        $("img").remove(".confirmAppointment");
        $(".reserveAPickupAppointmentRoommateAppointment").removeClass("confirm");
        $(this).addClass("confirm");
        $(this).append("<img src=images/confirmAppointment.png class=confirmAppointment id=roommateAppointment>");
            $(".confirmAppointment").click (function() {
                $(".confirmAppointment").unbind("click");
                var location = $(".reserveAPickupAppointmentLocation, this").text();
                alert (location);
            });
    }
   });

相关的php部分

echo '<table>';
while ($row = mysql_fetch_array($result)) { 

echo
'<tbody class = "reserveAPickupAppointmentRoommateAppointment">
    <tr>
            <td>'
                .$row["name"].
            '</td>
            <td>
                <span class = "reserveAPickupAppointmentLocation">'
                    .$row["location"].
                '</span>
            </td>
            <td>
                <span class = "reserveAPickupAppointmentSubLocation">'
                    .$row["subLocation"].
                '</span>
            </td>
        </tr>
        <tr>
            <td>
                <span class = "reserveAPickupAppointmentStartTime">'
                .$row["startTime"].
            '</span>    - 
            <span class = "reserveAPickupAppointmentEndTime">'
            .$row["endTime"].
                '</span>
            </td>
            <td>
                <span class = "reserveAPickupAppointmentDate">'
                .$row["date"].
                '</span>
            </td>
        </tr>
    </tbody>';
}

echo '</table>

3 个答案:

答案 0 :(得分:2)

要将第二个参数传递给jQuery选择器作为要搜索的上下文,您需要先关闭引号。

var location = $(".reserveAPickupAppointmentLocation", this).text();

<强> BUT

点击回调中的this值会改变值,因此您需要事先保存。

$(this).append("<img src=images/confirmAppointment.png class=confirmAppointment id=roommateAppointment>");
var self = this;
$(".confirmAppointment").click (function() {
    $(".confirmAppointment").unbind("click");
    var location = $(".reserveAPickupAppointmentLocation", self).text();
    alert (location);
});

答案 1 :(得分:1)

你的引号错了:

$(".reserveAPickupAppointmentLocaton", this).text();

this是jQuery函数的第二个参数,用于指定上下文,您将它作为选择器的一部分。 Context是jQuery

的可选参数

答案 2 :(得分:0)

试试这个:

var location = $(".reserveAPickupAppointmentLocaton", $(this)).text();