【问题标题】:JQuery clearing all text boxesJQuery清除所有文本框
【发布时间】:2020-06-23 10:51:43
【问题描述】:

我有一个问题。单击按钮后,我正在尝试清除表单的文本字段。 这是输入类型的 HTML:

<input type="button" id="clear_entries" value="Clear Entries">

这是我的 Jquery 代码:

$("#clear_entries").click(
                    function() {                        
                        $(":text").val("");                                 
            })
        

当我单击“清除条目”按钮时没有任何反应,但我不知道为什么。 我已在函数中使用警告声明(已从上述 sn-p 中删除)确认,以确认 正在调用函数。

【问题讨论】:

标签: jquery textbox eventhandler jquery-click-event


【解决方案1】:
$("#clear_entries").click(function(){                        
     $('input[type="text"]').val(""); 
 })

检查小提琴代码的链接

enter link description here

【讨论】:

    【解决方案2】:

    $('#clear_entries').on('click', function () {
      $(".form input[type=text]").val("");
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form class="form">
    <label for="">Name : </label> <input type="text">
    <label for="">email : </label> <input type="text">
    <input type="button" id="clear_entries" value="Clear Entries">
    </form>

    【讨论】:

      【解决方案3】:

      感谢大家的回复。我刚刚意识到我把它放在另一个按钮的功能中是错误的。也只是为了确认,两者

      $(":text").val(""); 
      

      $('input[type="text"]').val("");
      

      给出想要的结果。再次感谢您。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-08
        • 1970-01-01
        • 1970-01-01
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 2018-12-21
        相关资源
        最近更新 更多