使用jQuery删除值为x的输入框。

6

大家好,如何使用jquery删除特定值的输入框?

我知道应该使用.remove(),但是不知道如何找到特定的输入框。


可能是重复的问题:按值选择输入? - Felix Kling
7个回答

11
$("input[value='"+value+"']").remove();

你需要删除的元素的值是value。:)


7

循环遍历 <input> 标签,比对每一个值,若匹配,则删除该值:

$(document).ready(function() {
    $('input[type=text]').each(function() {
        if ($(this).val() === "foo") {
            $(this).remove();
        }
    });
});​

这里有演示jsFiddle的链接。


1
​$(function(){
    $("input[type='button']").click(function(){
        $("input[type='text']").each(function(){
           var $this = $(this);
            if ($this.val() == "x"){
               $this.remove();
            }                
        });
    });        
});​

http://jsfiddle.net/rZczZ/


0

0

我猜你是指输入框的值?

如果是这样,为什么不...

<input id="textField" type="text" value="testValue" />

$("#textField").val("");

那会清除文本框的值。


0

您可以使用此代码轻松删除特定的输入。

$(function(){
    $("input[type='button']").click(function(){
        $("input[value=x]").remove();
    });        
});

0

从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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接