简体   繁体   English

使用单选按钮将文本输入动态添加到列表中

[英]Add text input dynamically to list with radio buttons

I have a list (style taken from bootstrap) of elements, to which I want to add some more elements using text input. 我有一个元素列表(从引导程序中获取样式),我想使用文本输入向其中添加更多元素。 Here is my code- 这是我的代码-

<div class="col-lg-6">

    <div class="input-group">
      <input type="text" id = "input" class="form-control">
      <span class="input-group-btn">
      <button class="btn btn-default" type="button" id = "add" >Add</button>
      </span>

    </div><!-- /input-group -->
<form>
    <ul class="list-group" id = "tagList">

  <li class="list-group-item"> <b>Tags </b></li>  
  <li class="list-group-item"> <span class ="input-group-addon">
    <input type="radio">
    </span>Apple</li>
  <li class="list-group-item"> <span class ="input-group-addon">
    <input type="radio">
    </span> Orange</li>
  <li class="list-group-item"> <span class ="input-group-addon">
    <input type="radio">
    </span> Pear</li>
  <li class="list-group-item"> <span class ="input-group-addon">
    <input type="radio">
    </span> Banana</li>
</ul>
</form>


<script src="http://codeorigin.jquery.com/jquery-2.0.3.js"></script>

<script>

$('#add').click(function(){
    var text = $('#input').val();
    if(text.length){
        $('<li />', {html: text}).appendTo('ul.tagList')
    }
});

 </script>

I can get the code to work with a simpler list, but not with this one. 我可以使代码使用一个简单的列表,但不能与此列表一起使用。 Can anyone spot why not? 谁能发现为什么不呢?

Your li's are not closed: 您的李未关闭:

 <li class="list-group-item" <span class ="input-group-addon">
    <input type="radio">
    </span>Apple</li>

Should be: 应该:

 <li class="list-group-item"><span class ="input-group-addon">
    <input type="radio">
    </span>Apple</li>

Always funny to spot such issues once you placed the code into the code highlighting here on SO. 一旦将代码放入此处突出显示的代码中,发现这些问题总是很有趣。

In order for your javascript to work, you might use the following: http://jsfiddle.net/jX2K3/21/ 为了使您的JavaScript正常运行,您可以使用以下代码: http : //jsfiddle.net/jX2K3/21/

try following code.. 尝试下面的代码。

    $('#add').click(function(){
    var text = $('#input').val();
    if(text.length){        
    $('ul').append('<li class="list-group-item"><span class ="input-group-addon"> <input type="radio">    </span>'+text+'</li>');
    }
});

jsFiddle 的jsfiddle

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

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