2013-08-17 31 views
0

我创建了一个文本框,并将其添加到我的div:如何获得值,并移除文本框的jQuery

$('<input />').appendTo("#box") 

我想获得来自用户输入的值。

我也想删除文本框。我试过这个:

$('<input />').remove(); 

但没有运气。有任何想法吗?非常感谢。

+0

http://api.jquery.com/category/selectors/ – undefined

+1

的可能重复的[CSS选择文本输入字段?](http://stackoverflow.com/questions/4113965/css-selector -for-text-input-fields) – undefined

+0

js bin工作代码http://jsbin.com/aVuVedA/1/edit – Amith

回答

1

你会想尝试

$("#box").find("input").remove(); 

如果你有哪些你不希望在#box当你追加它删除,添加一个ID或类的input一些其他文本框到#box

$('<input />', { "class" : "remove-soon" }).appendTo("#box"); 

然后,您可以使用该类作为选择器来删除它。

$("#box").find(".remove-soon").remove(); 

为了让你会使用文本框的值,

$("#box").find(".remove-soon").val(); 
3
$(function() { 
    $('#box').on('change','input:text',function() { 
     var userInput = $(this).val(); 
     $(this).remove(); 
    }); 
}); 
相关问题