2012-05-10 40 views
3

夥計們如何使用jquery刪除具有特定值的輸入?Jquery刪除輸入的值x

我知道我應該用戶.remove()但我不知道如何找到具體的輸入。

+0

可能重複的[通過選擇值的輸入?](http://stackoverflow.com/questions/5084405/select-an-input-by-value) –

回答

4

循環到<input>區,然後相互匹配值,如果匹配,然後將其刪除:

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

演示這裏jsFiddle

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

哪裏value是要刪除的元素的value。 :)

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

'.filter()'是有用這裏也是:http://api.jquery.com/filter/ –

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> 
+0

段落不是輸入元素... –

+0

我編輯了代碼。 –

+0

'input'元素雖然沒有內容,所以':contains'對他們不起作用。 –