大家好,如何使用jquery删除特定值的输入框?
我知道应该使用.remove()
,但是不知道如何找到特定的输入框。
$("input[value='"+value+"']").remove();
你需要删除的元素的值是value
。:)
循环遍历 <input>
标签,比对每一个值,若匹配,则删除该值:
$(document).ready(function() {
$('input[type=text]').each(function() {
if ($(this).val() === "foo") {
$(this).remove();
}
});
});
这里有演示jsFiddle的链接。
$(function(){
$("input[type='button']").click(function(){
$("input[type='text']").each(function(){
var $this = $(this);
if ($this.val() == "x"){
$this.remove();
}
});
});
});
我猜你是指输入框的值?
如果是这样,为什么不...
<input id="textField" type="text" value="testValue" />
$("#textField").val("");
那会清除文本框的值。
您可以使用此代码轻松删除特定的输入。
$(function(){
$("input[type='button']").click(function(){
$("input[value=x]").remove();
});
});
从DOM中删除所有包含“hello”的输入。
<!DOCTYPE html>
<html>
<head>
<style>p { background:yellow; margin:6px 0; }</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p class="hello">Hello</p>
how are
<p>you?</p>
<button>Call remove(":contains('Hello')") on paragraphs</button>
<script>
$("button").click(function () {
$("input[type='text']").each(function(){
if($(this).val().toLowerCase().indexOf('hello')!=-1)
$(this).remove();
})
});
</script>
</body>
</html>
input
元素没有内容,因此:contains
无法在其上工作。 - Felix Kling