【问题标题】:Jquery Remove input with value xJquery 删除值为 x 的输入
【发布时间】:2012-05-10 10:04:34
【问题描述】:

各位,我如何使用 jquery 删除具有特定值的输入?

我知道我应该使用.remove(),但我不知道如何找到具体的输入。

【问题讨论】:

标签: jquery input


【解决方案1】:
$("input[value='"+value+"']").remove();

其中value 是您要删除的元素的value。 :)

【讨论】:

    【解决方案2】:

    循环进入<input>字段,然后匹配每个值,如果匹配,则将其删除:

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

    在这里演示jsFiddle

    【讨论】:

      【解决方案3】:
      ​$(function(){
          $("input[type='button']").click(function(){
              $("input[type='text']").each(function(){
                 var $this = $(this);
                  if ($this.val() == "x"){
                     $this.remove();
                  }                
              });
          });        
      });​
      

      http://jsfiddle.net/rZczZ/

      【讨论】:

        【解决方案4】:

        使用replaceWith() 函数。

        http://api.jquery.com/replaceWith/

        【讨论】:

          【解决方案5】:

          我假设您的意思是输入框值?

          在这种情况下为什么不...

          <input id="textField" type="text" value="testValue" />
          
          $("#textField").val("");
          

          这将清除文本框的值。

          【讨论】:

            【解决方案6】:

            您可以简单地使用此代码删除特定输入。

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

            【讨论】:

              【解决方案7】:

              从 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 对它们不起作用。
              猜你喜欢
              • 1970-01-01
              • 2017-08-20
              • 1970-01-01
              • 2012-05-07
              • 1970-01-01
              • 1970-01-01
              • 2013-05-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多