javascript jQuery从用户输入点击创建列表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4981765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:33:30  来源:igfitidea点击:

jQuery create list item from user input on click

javascriptjqueryhtml

提问by webwrks

Hi I am in need of some help. Please see my code below. Thanks in advance for any assistance.

嗨,我需要一些帮助。请在下面查看我的代码。在此先感谢您的帮助。

$('#input_listName').keyup(function(){
var newList = $(this).val();

$('#btn_createList').click(function(){
    ('.ul_current').append().html(newList);
});
});
<input type="text"  id="input_listName"/>
<br/>
<button type="submit" class="btn_sendMessage" id="btn_createList">Create List</button>

<ul class="ul_current">
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
</ul>

回答by jAndy

$('#btn_createList').click(function(){
    $('.ul_current').append($('<li>', {
         text: $('#input_listName').val()
    }));
});

should do it.

应该这样做。

Demo: http://www.jsfiddle.net/G8pbG/

演示http: //www.jsfiddle.net/G8pbG/

回答by netdreamer

$('#btn_createList').click(function() {    
    $('.ul_current').append('<li>' + $('#input_listName').val());
});

Demo: http://jsfiddle.net/netdreamer/uvuUe/

演示http: //jsfiddle.net/netdreamer/uvuUe/