如何选择另一个div内的另一个div?

时间:2015-10-09 14:08:28

标签: jquery

我有一个包含许多问题的页面,如下所示:

<div class='questionBlock'>
    <div class='questionBlockTitle '><span class='sectionQuestionBlockNumber'>4. </span><span class='sectionQuestionBlockTitle'>What can they do less of?</span>

        <div class='msgErrorRequired'></div>
        <div class='expandArrow downArrow'></div>
    </div>
    <div class='questionBlockExpand formQuestionPadding'>
        <div class='sectionQuestionBlockHelpText'></div>
        <input type='hidden' value='' name='13' mandatory='True' id='13' error=''>
        <div style='width:700px;height:100px;' class='sectionItemBlock '> <span class='questionControl'>
                <textarea  id='qt_13' name='qt_13' style='width:695px;height:100px;' maxlength='100' question='13' disabled='disabled' ></textarea>
            </span>

        </div>
    </div>
</div>

然后我有一些JQuery检查页面上的所有textareas,如果找到一个没有填写的,则修改错误消息div以显示错误。

我无法引用此错误消息div。

到目前为止,我已经得到了这个:

$("#" + value).parents(".questionBlockExpand").parents(".questionBlock").siblings(".msgErrorRequired").html(msgErrorRequired);

但它似乎不起作用。

我是否正确引用它?

5 个答案:

答案 0 :(得分:3)

你的问题似乎是因为.msgErrorRequired不是兄弟姐妹。 questionBlock。尝试:

self.WhiteBtn.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:0.9];
self.WhiteBtn.opaque = NO;

self.WhiteBtn.clipsToBounds = NO;

self.WhiteBtn.layer.shadowColor = [UIColor blackColor].CGColor;
self.WhiteBtn.layer.shadowOpacity = 0.5;
self.WhiteBtn.layer.shadowRadius = 5;
self.WhiteBtn.layer.shadowOffset = CGSizeMake(0,0);

你也可以简化这个:

$("#" + value).parents(".questionBlockExpand").parents(".questionBlock").find(".msgErrorRequired").html(msgErrorRequired);

答案 1 :(得分:1)

这里试试这个:

编辑:反映评论的优化

$("#" + value).parents(".questionBlock")
              .find(".msgErrorRequired").html(msgErrorRequired);

答案 2 :(得分:1)

这样可行:

&#13;
&#13;
@zones = Zone.es.search(
    body: {
      query: {
        bool: {
          should: [
            {match: {city: search}},
            {match: {zipcode: search.to_i}}
          ]
        }
      },
      size: limit
    })
&#13;
$('.noEmpty').each(function() {
  var msgErrorRequired = 'You can\'t leave this field empty!';
  var $this = $(this);
  if ($this.val().trim() == '') {
    $this.closest(".questionBlock").find(".msgErrorRequired").html(msgErrorRequired);
  }

});
&#13;
.msgErrorRequired {
  color: red;
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

这是我的建议 - 写的,而其他人则添加了他们的

&#13;
&#13;
$(function() { // can be on submit
  $("textarea[name^='qt']").each(function() {
    var val = $.trim(this.value);
    $(this).closest(".questionBlock")
    .find(".msgErrorRequired")
    .html(val==""?"Please fill in":"");
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='questionBlock'>
  <div class='questionBlockTitle '><span class='sectionQuestionBlockNumber'>4. </span><span class='sectionQuestionBlockTitle'>What can they do less of?</span>

    <div class='msgErrorRequired'>sss</div>
    <div class='expandArrow downArrow'></div>
  </div>
  <div class='questionBlockExpand formQuestionPadding'>
    <div class='sectionQuestionBlockHelpText'></div>
    <input type='hidden' value='' name='13' mandatory='True' id='13' error=''>
    <div style='width:700px;height:100px;' class='sectionItemBlock '> <span class='questionControl'>
                <textarea  id='qt_13' name='qt_13' style='width:695px;height:100px;' maxlength='100' question='13'  >This is not empty</textarea>
            </span>

    </div>
  </div>
<div class='questionBlock'>
  <div class='questionBlockTitle '><span class='sectionQuestionBlockNumber'>5. </span><span class='sectionQuestionBlockTitle'>What can they do more of?</span>

    <div class='msgErrorRequired'>sss</div>
    <div class='expandArrow downArrow'></div>
  </div>
  <div class='questionBlockExpand formQuestionPadding'>
    <div class='sectionQuestionBlockHelpText'></div>
    <input type='hidden' value='' name='14' mandatory='True' id='14' error=''>
    <div style='width:700px;height:100px;' class='sectionItemBlock '> <span class='questionControl'>
                <textarea  id='qt_14' name='qt_14' style='width:695px;height:100px;' maxlength='100' question='14'  ></textarea>
            </span>

    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:-1)

这个怎么样:

$("#" + value + ' .questionBlockExpand .msgErrorRequired').html(msgErrorRequired);