javascript - 查找最近/下一个 div

标签 javascript jquery

我在 <td> 下有 2 个 div标签:

 // first
<td>
  <div class="date">$row[11]</div>
</td>
<td class="status1">
    <div class="question">
      <form action="update.php" method="post" id="form-id" enctype="multipart/form-data">
       <div class="recstatus">$row[12]</div>
        <input type="hidden" value="$row[12]" name="recstatus">
        <input type="hidden" value=$row[1] name="audit_name">
       <select name="status" class="select" >
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option   selected="selected">choose</option>
       </select>


      <div class="subquestion">
       <label>upload file</label>
       <input type="file" name="file">
       <input type="submit" name="submit">
      </div>

     </form><br/>
   </div>

    //second
   <div class="question1">
        <form action="update.php" method="post" id="form-id" enctype="multipart/form-data">
          <input type="hidden" value=$row[1] name="audit_name">
         <select name="status1" >
          <option value="4">4</option>
          <option  selected="selected">choose</option>
         </select>
         <div class="subquestion1">
          <label>upload file</label>
          <input type="file" name="file">
          <input type="submit" name="submit">
         </div>
        </form>

   </div>
 </td>

这里是 JQuery:

 $(document).ready(function () {
    $('.date').each(function () {
        $('.question1').hide();
        var date = new Date($(this).text().replace("-", "/"));
        var recstatus = $(this).closest('td').next().find('.recstatus');
        if (new Date() > date && recstatus.text() == 3) {
            recstatus.addClass('invaliddate');
            //if this is true then hide first div and show second div
            // something like this.. but this isn't working 
            // i need closest/next div (this divs are in cycle)
            $(this).closest("div").find(".question1").show();
            $(this).closest("div").find(".question").hide();
        }
    });
});

我想显示question1 div if(这个jquery if语句为真)并隐藏question div 但它们在<td>下在循环中,所以我想使用下一个/最近的函数

最佳答案

您需要找到父 td 元素,然后找到下一个同级 td 元素,您需要在其中找到相关的 div

$(this).closest("td").next().find(".question1").show();
$(this).closest("td").next().find(".question").hide();

关于javascript - 查找最近/下一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21362693/

相关文章:

javascript - 方法中的默认变量?

javascript - 将 jquery 集合添加到 jQuery 集合时使用什么方法?

Javascript 获取元素 CSS 过渡阶段

javascript - 使用 jquery 验证动态输入字段

javascript - 如何使用ajax和mysql循环数据

javascript - 如何使用 jquery 调整 html 表的行和列的大小?

javascript - 如何附加到另一个页面上的选择下拉列表?

javascript - 一次一个标记动画 Google Maps Api 3

javascript - HTML5 History API 初始加载问题

javascript - 是否可以使用 href "Go Back"通过我的 ='javascript:history.go(-1)' 按钮发送变量