[英]How can I select the correct child element hidden input value based upon the context of the current selected button?
I trying to put together a 5 day checklist app. 我试图将5天的清单应用程序放在一起。 The check list app has 5 divs that span across the page horizontally.
核对清单应用程序具有5个div,这些div横跨整个页面。 Each div represents a day.
每个div代表一天。 Each div represents a day of the week and each div contains a hidden input field which holds a date value for for that particular day.
每个div代表星期几,每个div都包含一个隐藏的输入字段,其中包含该特定日期的日期值。 Each div also has a check-list button and when the user clicks on that button the task list is removed and a form appears in its place to add a task.
每个div都有一个检查列表按钮,当用户单击该按钮时,任务列表将被删除,并在其位置出现一个表单以添加任务。 I'm trying to capture the value of the hidden input field, which is the date for that particular div.
我正在尝试捕获隐藏的输入字段的值,这是该特定div的日期。 However, even though I'm using the "this" keyword, no matter which check-list button I click, I'm getting back the current date only.
但是,即使我使用“ this”关键字,无论我单击哪个检查清单按钮,我都只会获得当前日期。 Which is the input value in the first div.
这是第一格的输入值。 How can I select the unique input value(the date) based upon which check-list button I click?
如何根据单击的清单按钮选择唯一的输入值(日期)?
jquery: jQuery的:
$(".task-btn").click(function(){
// Setting form to the clone I want to acces in .on()
var refdate = $(this).closest('#replace').children('.hidden-date').val();
console.log(refdate);
});
html and php: html和php:
<?php
$date = date("F j, Y");
$refDate = date("Y-m-d");
$counterForLi = 0;
?>
<div id="replace">
<input type="hidden" name="thedate" class="hidden-date" value="<?php echo $refDate; ?>">
<h2 class="day"></h2>
<h4 class="thedate">
<?php
if ($counter > 0) {
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+$counter, date("y"));
$refDate = date("Y-m-d", $tomorrow);
$date = date("F j, Y", $tomorrow);
echo $date;
} else {
echo $date;
}
?>
</h4>
<?php echo $refDate; ?>
<div class="btn-group">
<button class="btn task-btn">Add Task</button>
Note: I echoed out the date for each div and made sure there wasn't a problem with the php. 注意:我回显了每个div的日期,并确保php没有问题。
Although the answer to your question can be: 尽管您的问题的答案可以是:
$(this).parents("#replace").find('.hidden-date').val();
Your problem is somewhere in another place because the snippet you shown should also work 您的问题在另一个地方,因为您显示的摘录也应该有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.