使用jquery clone()在输入字段中生成一类新的bootstarp Tokenfield

http://jsfiddle.net/rns3hang/450/

新克隆的输入字段中的TokenField正在工作,但问题是它是否在输入字段中

我只想要一个带有一些预定义/常量标签的输入字段 ,并且还能够添加新的令牌或标签值

 


Insert Diet



`
Breakfast Pre Lunch Lunch

Jquery代码

 $(document).ready(function(){ $('.skill').tokenfield({ autocomplete:{ source: ['Food1','Food2','Food3','Food4','Food5','CSS','Laravel','CakePHP'], delay:100 }, showAutocompleteOnFocus: true }); var count = 1; var $table; $table=$('#crud_table tbody'); var $existRow=$table.find('tr').eq(1); /* bind to existing elements on page load*/ bindAutoComplete($existRow); $('#add').click(function(){ //$("#crud_table tbody tr.tableinputs:last").clone().appendTo("#crud_table tbody"); var $row=$("#crud_table tbody tr.tableinputs:last").clone(); var $input=$row.find(":text").val(""); $table.append($row); /* $(".break_name:last").val(''); $(".mid_meal:last").val(''); $(".lunch_name:last").val('');*/ bindAutoComplete($row ); $input.focus(); }); function bindAutoComplete($row ){ /* use row as main element to save traversing back up from input*/ $row.find('.skill').tokenfield({ autocomplete:{ source: ['Food1','Food2','Food3','Food4','Food5','CSS','Laravel','CakePHP'], delay:100 }, showAutocompleteOnFocus: true }); $('.skill').on('tokenfield:createtoken', function (event) { var existingTokens = $(this).tokenfield('getTokens'); $.each(existingTokens, function(index, token) { if (token.value === event.attrs.value) event.preventDefault(); }); }); } $(document).on('click', '.remove', function(){ //If this there is only one row left clear that row instead of removing it if ($("#crud_table tbody tr").length === 1) $("#crud_table tbody tr:last").find("input").val(''); else $("#crud_table tbody tr:last").remove(); }); }); 

我尝试从输入字段中删除value属性但它仍然无效

我只想要一个输入字段

由于bootstrap-tokenfield的逻辑正在使用它,这有点挑战。 但我得到了一个可能的解决方案,可能会让你高兴。 我们尊重创建的新输入字段,但删除属性“name”,因此表单会忽略它。

之前:

 $('.skill').on('tokenfield:createtoken', function (event) { var existingTokens = $(this).tokenfield('getTokens'); $.each(existingTokens, function(index, token) { if (token.value === event.attrs.value) event.preventDefault(); }); }); 

 $('.skill').on('tokenfield:createtoken', function (event) { var existingTokens = $(this).tokenfield('getTokens'); $.each(existingTokens, function(index, token) { if (token.value === event.attrs.value) event.preventDefault(); }); $(this).data('bs.tokenfield').$input.attr("name",""); //<- this is the new line }); 
Interesting Posts