动态文本框,Jquery

时间:2014-03-10 13:22:44

标签: javascript jquery

<a href="#" id="AddMoreFileBox" class="btn btn-info">Add More Field</a>
<div id="InputsWrapper">
<div><input type="text" name="mytext[]" id="field_1" value="Text 1"><a href="#" class="removeclass">&times;</a></div>
</div>

脚本:

<script type='Javascript'>
$(document).ready(function() {

var MaxInputs       = 8; //maximum input boxes allowed
var InputsWrapper   = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton       = $("#AddMoreFileBox"); //Add button ID

var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added

$(AddButton).click(function (e)  //on add input button click
{
        if(x <= MaxInputs) //max input box allowed
        {
            FieldCount++; //text box added increment
            //add input box
            $(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/><a href="#" class="removeclass">&times;</a></div>');
            x++; //text box increment
        }
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text
        if( x > 1 ) {
                $(this).parent('div').remove(); //remove text box
                x--; //decrement textbox
        }
return false;
}) 

});

</script>

我在jfiddle上找到了这个。我在浏览器上尝试过它不会添加。

是否有加载库的声明?

http://jsfiddle.net/LYDuZ/

1 个答案:

答案 0 :(得分:0)

好像你忘记在代码中引用jquery了。在头标记中添加此行

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>