I have a single text field and an accompanying "add" button:
我有一个文本框和一个附加的“添加”按钮:
<fieldset id="itemList">
<input type="text" id="addItem" name="addItem">
<input type="button" name="add" value="add">
</fieldset>
Whenever someone clicks "add" I'd like to add the new item to the list below and clear the input field above.
每当有人点击“添加”时,我都想将新项添加到下面的列表中,并清除上面的输入字段。
<hr>
<ol id="resultList">
<li>just example</li>
<li>new text input</li>
<li>last entered item</li>
</ol>
I'm not sure how to accomplish this in jQuery. Can you help?
我不知道如何用jQuery实现这一点。你能帮助吗?
For bonus, I'd like to limit the list to 10. So I need to somehow count the number of list items and make the above form's display contingent on <11 list items.
另外,我想把名单限制在10人以内。因此,我需要计算列表项的数量并使上面的表单的显示取决于<11个列表项。
Can you help?
你能帮助吗?
3 个解决方案
#1
1
jQuery(function($){
$('input[name="add"]').click(function() {
if($('#resultList li').length < 10)
{ value = $('input[name="addItem"]').val();
$('ol li:last').after("<li>" + value + "</li>");
$('input[name="addItem"]').val(""); }
if($('#resultList li').length == 10)
{ $('input[name="addItem"]').css("display", "none");
$('input[name="add"]').css("display", "none"); }
});
});
Working jsFiddle here: http://jsfiddle.net/bhZdz/2/
在这里工作jsFiddle:http://jsfiddle.net/bhZdz/2/
Edited because I didn't see the last part of your question, form hides after 10th element is added now.
编辑因为我没有看到你问题的最后一部分,表单隐藏在第10个元素之后。
#2
1
function addItem(){
var list = $("#resultList");
var count = list.find("li").size();
if(count < 10){
var input = $("input[name=addItem]");
var item = input.val();
list.append(("<li>"+item+"</li>"));
input.val(" "); // clear field
}else{
// already have 10 items
}
}
$("input[name=add]").click(addItem);
#3
1
Try this
试试这个
$('input[type=button]').click(function(){
$('#resultList').append('<li>'+$('#addItem').val()+'</li>');
$('#addItem').val("");
}
);
You can also use input[name=add]
您还可以使用input[name=add]
#1
1
jQuery(function($){
$('input[name="add"]').click(function() {
if($('#resultList li').length < 10)
{ value = $('input[name="addItem"]').val();
$('ol li:last').after("<li>" + value + "</li>");
$('input[name="addItem"]').val(""); }
if($('#resultList li').length == 10)
{ $('input[name="addItem"]').css("display", "none");
$('input[name="add"]').css("display", "none"); }
});
});
Working jsFiddle here: http://jsfiddle.net/bhZdz/2/
在这里工作jsFiddle:http://jsfiddle.net/bhZdz/2/
Edited because I didn't see the last part of your question, form hides after 10th element is added now.
编辑因为我没有看到你问题的最后一部分,表单隐藏在第10个元素之后。
#2
1
function addItem(){
var list = $("#resultList");
var count = list.find("li").size();
if(count < 10){
var input = $("input[name=addItem]");
var item = input.val();
list.append(("<li>"+item+"</li>"));
input.val(" "); // clear field
}else{
// already have 10 items
}
}
$("input[name=add]").click(addItem);
#3
1
Try this
试试这个
$('input[type=button]').click(function(){
$('#resultList').append('<li>'+$('#addItem').val()+'</li>');
$('#addItem').val("");
}
);
You can also use input[name=add]
您还可以使用input[name=add]