简体   繁体   English

创建元素在jquery 1.8.3中不起作用

[英]Create Element Doesn't work in jquery 1.8.3

I Have used jquery 1.8.3 to Dynamically Create the Element. 我使用jquery 1.8.3动态创建元素。 But It doesn't working on this. 但它没有解决这个问题。 but It's working on 1.3.2 version. 但它正在开发1.3.2版本。 Below is My Jquery Code Which I Have used for that. 下面是我用过的My Jquery Code。

 $(document).ready(function(){

        var counter = 2;

        $("#addButton").click(function () {

        if(counter>10){
                alert("Only 10 textboxes allow");
                return false;
        }   

        var newTextBoxDiv = $(document.createElement('div'))
             .attr("id", 'CallBackDiv' + counter);

        newTextBoxDiv.after().html('<label>Call Back Date Time #'+ counter + ': </label>'+
              '<div class="controls"><input type="text" name="callback' + counter + 
              '" id="callback' + counter + '" value="" ></div>');

        newTextBoxDiv.appendTo("#control-group");


        counter++;
         });

         $("#removeButton").click(function () {
        if(counter==1){
              alert("No more textbox to remove");
              return false;
           }   

        counter--;

            $("#CallBackDiv" + counter).remove();

         });

         $("#getButtonValue").click(function () {

        var msg = '';
        for(i=1; i<counter; i++){
          msg += "\n CallBack #" + i + " : " + $('#callback' + i).val();
        }
              alert(msg);
         });
      });

<div id='control-group'>
    <div id="CallBackDiv1">
<label class="control-label" for="input01">
Call Back Date Time #1:</label><div class="controls">
<input type='textbox' id='callback1' ></div>
    </div>
</div>
<input type='button' value='+' id='addButton'>
<input type='button' value='-' id='removeButton'>

There's nothing wrong with the way you are creating the div . 你创建div的方式没有错。 The problem with your code is your use of jQuery's after() function. 你的代码的问题是你使用jQuery的 after()函数。 Calling after() on a newly created element will return null , which you are then calling the html() method on. 在新创建的元素上调用after()将返回null ,然后您将调用html()方法。 I suggest you read up on how to use the after() function -> http://api.jquery.com/after/ 我建议你阅读如何使用after()函数 - > http://api.jquery.com/after/

why dont you do it in plain js 为什么不用普通的js做

var newTextBoxDiv = document.createElement('div');
newTextBoxDiv.setAttribute("id", 'CallBackDiv' + counter);

Using jQuery you can dynamically create elements like this: 使用jQuery,您可以动态创建如下元素:

// Create a new div
var newTextBoxDiv = $('<div/>', {
    id: 'CallBackDiv' + counter
});    

// Append the new element to the DOM
newTextBoxDiv.appendTo("#control-group");

// You can check the console for the 'newTextBoxDiv' id
console.log(newTextBoxDiv.attr('id'));

// Then call the after part
newTextBoxDiv.after().html('...');

FIDDLE DEMO FIDDLE DEMO

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

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