简体   繁体   English

如何根据当前所选按钮的上下文选择正确的子元素隐藏的输入值?

[英]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.

相关问题 如何使用 JavaScript\\JQuery 使用选择标记的选定选项的值设置隐藏输入标记的值? - How can I set the value of an hidden input tag with the value of the selected option of a select tag using JavaScript\JQuery? 如何使用敲除.js根据现有数组中选择元素的选择值设置输入字段的可见性 - how to set the visibility of a input field based upon the selected value of a select element within an existing array using knockout.js 如何监控隐藏输入元素的值? - How can I monitor the value of a hidden input element? 当我有一个隐藏的字段(如果选择了一个选项时会显示)时,我无法获得select元素的值 - I can't get the value of my select element when i have a hidden field that is revealed if an option is selected 如何从以下获取所选按钮的当前值? - How can I get current Value of Selected button from the following? 如何根据子元素值对父元素排序? - How can I sort a parent element based on child element value? 如何根据选定的单选按钮更新选择选项? - How can I update select options based on selected radio button? 如何更改选定单选按钮的输入类型隐藏值 - How to change a input type hidden value of a selected radio button 根据所选值传递隐藏输入 - Passing hidden input based on selected value 如何将jquery multiselect下拉列表的选定值放入隐藏元素的ID中 - How can I put selected value of jquery multiselect dropdown into a hidden element's id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM