简体   繁体   English

jQuery儿童的子元素

[英]Jquery Children's child element

$(parentQuestion.children('.choice_class:last'));
<div class='parentquestion'> .....
<div class="choice_class">
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12"><span class="choice-no">Option 1</span><span class="required">*</span>
                </label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <input type="text" required="required" class="form-control col-md-7 col-xs-12">
                </div>
                <a href="javascript: void(0);" onclick="addOption(this)" class="btn btn-danger"><span class="glyphicon glyphicon-plus"></span>Add Choice</a>
            </div>
        </div>

inside addOption function, using the this variable, I found the parent object and added another option below the last .choice_class. 在addOption函数中,使用this变量,我找到了父对象,并在最后一个.choice_class下面添加了另一个选项。

I need to change the Option 1 to Option 2 when the second option is added. 添加第二个选项后,我需要将选项1更改为选项2。

The complete function is 完整的功能是

function addOption(that) {
      $(that).parent().parent().after($('.choice_class_sample .choice_class').clone());

      var parentQuestion = $(that).parent().parent().parent();
      var lastChoice = parentQuestion.children('.choice_class:last');

      $(parentQuestion.children('.choice_class:last-child .choice-no')).html(parentQuestion.children('.choice_class').length);
      $(that).remove();
  }

But I am not able to select further more after selecting the children. 但是选择孩子后,我无法再选择更多。

I could modify your code to "make it work", but the way you're going about this is a little bit unorthodox – namely, using clone to copy the existing element before duplicating it. 我可以修改您的代码以使其“正常工作”,但是您要这样做的方式有点不合常规-即在复制之前使用clone复制现有元素。

If you're going to be representing a JavaScript data structure (like an array) in DOM, and wanting to keep them in sync, it'd be much better to use a templating system. 如果要在DOM中表示JavaScript数据结构(如数组),并希望使其保持同步,那么最好使用模板系统。 Since you're using jQuery, the jQuery Template plugin seems decent. 由于您使用的是jQuery,因此jQuery Template插件看起来不错。 From skimming the README, it would make your stuff as simple as this: 通过阅读自述文件,可以使您的工作变得如此简单:

<script type="text/html" id="template">
  <div>
    <div>
      <label>
        <span>Option <span data-content="id"></span></span>
        <span class="required">*</span>
      </label>

      <div>
        <input type="text" required="required">
      </div>

      <a href="#" class="add-choice-button"><span class="glyphicon glyphicon-plus"></span> Add Choice</a>
    </div>
  </div>
</script>

Then, in your JavaScript, you'd have something like: 然后,在您的JavaScript中,您将得到以下内容:

var options = [ { id: 1 } ]

parentQuestion.loadTemplate("#template", questions)

$('.add-choice-button').on('click', function () {
  options.push({
    id: options.length + 1  
  })

  parentQuestion.loadTemplate('#template', options)
})

Much, much cleaner. 干净得多。 And best of all, it avoids having ugly, imperative DOM manipulation in your JS. 最重要的是,它避免了在JS中进行难看的命令式DOM操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM