在我的代码中,我有这个循环,它会带来各种2小时的时间块。我发现很难从.clientSearchSectionButtonHiddenTextEndTime中获取文本。我从.clientSearchSectionButtonHiddenTextStartTime中获取文本没有任何问题。
PHP
while($row = mysql_fetch_array($result)) {
$startTimeVar = $row["startTime"];
$startTimeCorrected = date("g:i a", strtotime($startTimeVar));
$endTimeVar = $row["endTime"];
$endTimeCorrected = date("g:i a", strtotime($endTimeVar));
echo
'<div class = "clientSearchSectionButtonText">
<div class = "clientSearchSectionButtonTextTime">
'.$startTimeCorrected.' - '.$endTimeCorrected.'
</div>
</div>
<span class = "clientSearchSectionButtonHiddenTextStartTime" style = "display: none;">
'.$row["startTime"].'
</span>
<span class = "clientSearchSectionButtonHiddenTextEndTime" style = "display: none;">
'.$row["endTime"].'
</span>
<div class = "buttonBreak">
</div>';
}
JQuery - 我试图以多种方式选择它。 $ this给出div clientSearchSectionButtonTextTime。我尝试了很多不同的东西,包括.parent()。next,parent()。sibling,.sibling ... etc
/*----SELECT TIME----*/
$(".clientSearchSectionButtonTextTime").live("click", function() {
startTime = $(this).parent().next(".clientSearchSectionButtonHiddenTextStartTime").text();
endTime = $(this).parent().next(".clientSearchSectionButtonHiddenTextEndTime").text();
alert (endTime);
});
答案 0 :(得分:2)
果然,.next()
只选择下一个兄弟。如果给出了一个选择器,则测试该单个兄弟以匹配它,否则结果为空。
尝试.nextAll('.uncannyClassName')
,你应该好好去。
/*----SELECT TIME----*/
$(".clientSearchSectionButtonTextTime").live("click", function() {
startTime = $(this).parent().nextAll(".clientSearchSectionButtonHiddenTextStartTime").text();
endTime = $(this).parent().nextAll(".clientSearchSectionButtonHiddenTextEndTime").text();
alert (endTime);
});
我也将它添加到.clientSearchSectionButtonHiddenTextStartTime
,以便将来可以扩展到DOM(而不仅仅是选择下一个元素)。
答案 1 :(得分:1)
如果你不需要看到你可以使用像这样隐藏的输入
<input type="hidden" name="clientSearchSectionButtonHiddenTextStartTime[]" value="<?php echo $row['startTime']; ?>" />
<input type="hidden" name="clientSearchSectionButtonHiddenTextEndTime[]" value="<?php echo $row['endTime']; ?>" />
但是回到你的麻烦你可以在你的html中做这样的事情,只是为了chiarify
while($row = mysql_fetch_array($result)) {
$startTimeVar = $row["startTime"];
$startTimeCorrected = date("g:i a", strtotime($startTimeVar));
$endTimeVar = $row["endTime"];
$endTimeCorrected = date("g:i a", strtotime($endTimeVar));
echo '<div class="group">
<div class="clientSearchSectionButtonText">
<div class="clientSearchSectionButtonTextTime">'.$startTimeCorrected.' - '.$endTimeCorrected.'</div>
</div>
<span class="clientSearchSectionButtonHiddenTextStartTime" style="display: none;">'.$row["startTime"].'</span>
<span class="clientSearchSectionButtonHiddenTextEndTime" style="display: none;">'.$row["endTime"].'</span>
<div class="buttonBreak"></div>
</div>';
}
对每条记录进行分组,这样您就可以像这样使用jquery
/*----SELECT TIME----*/
$(".clientSearchSectionButtonTextTime").live("click", function() {
startTime = $(this).parent('groups').find(".clientSearchSectionButtonHiddenTextStartTime").text();
endTime = $(this).parent('groups').find(".clientSearchSectionButtonHiddenTextEndTime").text();
alert (endTime);
});
我没试过,但应该没问题
答案 2 :(得分:0)
尝试使用.find()而不是.next()
例如:
endTime = $(this).parent().find(".clientSearchSectionButtonHiddenTextEndTime").text();
答案 3 :(得分:0)
echo '
<div name="'.$count.'" class = "clientSearchSectionButtonText">
<div class = "clientSearchSectionButtonTextTime">
'.$startTimeCorrected.' - '.$endTimeCorrected.'
</div>
</div>
<span name="'.$count.'" class = "clientSearchSectionButtonHiddenTextStartTime" style = "display: none;">
'.$row["startTime"].'
</span>
<span name="'.$count.'" class = "clientSearchSectionButtonHiddenTextEndTime" style = "display: none;">
'.$row["endTime"].'
</span>
<div class = "buttonBreak">
</div>';
$(".clientSearchSectionButtonTextTime").live("click", function() {
name = $(this).getAttribute("name");
startTime = $('.clientSearchSectionButtonHiddenTextStartTime[name="'+name+'"]');
});
自从我编写自己的CSS选择器以来已经有一段时间了,但你应该能够理解这一点 在每个div / spans上放置一个公共标识符,比尝试浏览DOM更容易。
答案 4 :(得分:0)
如果next()
和closest()
解决方案不起作用,则需要调试代码。首先执行此操作,看看它是否有效:
/*----SELECT TIME----*/
$(".clientSearchSectionButtonTextTime").live("click", function() {
alert ($(this).parent().next(".clientSearchSectionButtonHiddenTextStartTime").text());
//startTime = $(this).parent().next(".clientSearchSectionButtonHiddenTextStartTime").text();
//endTime = $(this).parent().next(".clientSearchSectionButtonHiddenTextEndTime").text();
});