简体   繁体   English

jQuery替换并附加

[英]jQuery replace with and append

I have a simple piece of code that changes a select box into a text field if the user selects yes. 我有一段简单的代码,如果用户选择是,则将选择框更改为文本字段。 I then want to be able to change it back to a drop down if if the user checks the box besides the text field. 然后,如果用户选中了文本字段旁边的框,那么我希望能够将其更改回下拉菜单。

The issue however is that when it replaces the element, it doesn't append the checkbox to it. 但是,问题在于,当替换元素时,不会将复选框附加到该元素上。

$(document).on("change", "[name=projectRelated]", function(e) {

    // Define our values
    var prj = this.value;

    // Depending on our choice
    switch(prj){
        case 'Yes':
            $(this)
            .replaceWith($('<input/>',{'type':'text','value':'', 'name':'projectRelatedDesc', 'placeholder':'Enter project details'}))
            .append('<div class="checkbox"><label><input type="checkbox" class="notPrjRelated"> No</label>');
        break;
    }
});

Here is a fiddle: https://jsfiddle.net/0ytut7ec/ 这是一个小提琴: https : //jsfiddle.net/0ytut7ec/

What am I missing? 我想念什么?

Try to use .after() at this context. 尝试在这种情况下使用.after() Also the order of execution is also important. 执行顺序也很重要。

If you add a new element after removing/replacing the target element, then that new element will not be added as the target lost its reference in DOM. 如果在删除/替换目标元素后添加新元素,则该新元素将不会添加,因为目标在DOM中丢失了其引用。

$(document).on("change", "[name=projectRelated]", function(e) {
  // Define our values
  var prj = this.value;
  // Depending on our choice
  switch (prj) {
    case 'Yes':
     $(this)
     .after('<div class="checkbox"><label><input type="checkbox" class="notPrjRelated"> No</label>')
        .replaceWith($('<input/>', {
          'type': 'text',
          'value': '',
          'name': 'projectRelatedDesc',
          'placeholder': 'Enter project details'
        }))
      break;
  }
});

DEMO DEMO

 $(document).on("change", "[name=projectRelated]", function(e) { // Define our values var prj = this.value; // Depending on our choice switch (prj) { case 'Yes': $(this).after('<div class="checkbox"><label><input type="checkbox" class="notPrjRelated"> No</label>').end() .replaceWith($('<input/>', { 'type': 'text', 'value': '', 'name': 'projectRelatedDesc', 'placeholder': 'Enter project details' })) break; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="span2" name="projectRelated"> <option value="">Select Option</option> <option value="Yes">Yes</option> <option value="No">No</option> </select> 

Offering an alternate answer: https://jsfiddle.net/Twisty/6mx4r3s8/ 提供替代答案: https : //jsfiddle.net/Twisty/6mx4r3s8/

$(document).on("change", "[name=projectRelated]", function(e) {

  // Define our values
  var prj = this.value;

  // Depending on our choice
  switch (prj) {
    case 'Yes':
      var $inp = $('<input/>', {
        'type': 'text',
        'value': '',
        'name': 'projectRelatedDesc',
        'placeholder': 'Enter project details'
      });
      var $chk = $('<div class="checkbox"><label><input type="checkbox" class="notPrjRelated">No</label></div>');
      $(this).replaceWith($inp);
      $inp.after($chk);
      break;
  }
});

Here we create the input and the checkbox, replace, and then append after. 在这里,我们创建输入和复选框,替换,然后追加。

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

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