简体   繁体   English

使用jQuery + CSS类向Bootstrap 3输入添加“ Clear Field”按钮

[英]Adding “Clear Field” buttons to Bootstrap 3 inputs using jQuery + CSS class

How can I add a "Clear field" button to multiple Bootstrap 3 input fields just using jQuery and a CSS class? 如何仅使用jQuery和CSS类将“清除字段”按钮添加到多个Bootstrap 3输入字段?

I've found solutions that can add a 'clear field' button to a field with a particular ID, but nothing so far that can do it by class. 我找到了可以向具有特定ID的字段添加“清除字段”按钮的解决方案,但到目前为止,还没有任何一种方法可以按类进行操作。 I've got a form with a lot of fields and I'd rather not have to repeat my code over again for each field. 我有一个包含很多字段的表单,我不想不必为每个字段重复我的代码。

I've tried this so far (Bootply), but I can't figure out how to get jQuery to clear just the one field and toggle the one icon, not all of them. 到目前为止,我已经尝试过(引导),但是我不知道如何让jQuery清除一个字段并切换一个图标,而不是全部。

//JS // JS

        $(document).ready(function(){
            $(".searchinput").keyup(function(){
                $(".searchclear").toggle(Boolean($(this).val()));
            });
            $(".searchclear").toggle(Boolean($(".searchinput").val()));
            $(".searchclear").click(function(){
                $(".searchinput").val('').focus();
                $(this).hide();
            });
        });

//HTML // HTML

       <div class="btn-group">
         <input id="searchinput" type="search" class="form-control searchinput" placeholder="type something..." value="">
         <span id="searchclear" class="searchclear glyphicon glyphicon-remove-circle">           </span>
       </div>

       <div class="btn-group">
         <input id="searchinput" type="search" class="form-control searchinput" placeholder="type something..." value="">
         <span id="searchclear" class="searchclear glyphicon glyphicon-remove-circle"></span>
       </div>

//CSS // CSS

.searchinput {
    width: 200px;
}

.searchclear {
    position:absolute;
    right:5px;
    top:0;
    bottom:0;
    height:14px;
    margin:auto;
    font-size:14px;
    cursor:pointer;
    color:#ccc;
}

1) You can use $(this) to get a reference to the current targeted element 1)您可以使用$(this)获得对当前目标元素的引用

2) Use .next() to toggle the visibility for only the icon which is the next immediate sibling of input that you're currenlty key in 2)使用.next()切换仅图标的可见性,这是​​您当前input的下一个直接input

3) Use .prev() to clear only the input which is the immediate previous sibling of clear icon that is clicked: 3)使用.prev()仅清除input ,该input是单击的清除图标的前一个同级:

Final code should look like: 最终代码应如下所示:

$(document).ready(function () {
    $(".searchinput").keyup(function () {
        $(this).next().toggle(Boolean($(this).val()));
    });
    $(".searchclear").toggle(Boolean($(".searchinput").val()));
    $(".searchclear").click(function () {
        $(this).prev().val('').focus();
        $(this).hide();
    });
});

Bootply Demo 引导演示

Try changing your javascript code to this: 尝试将您的JavaScript代码更改为此:

        $(document).ready(function(){
            $(".searchinput").keyup(function(){
                $(this).parent().find('.searchclear').toggle(Boolean($(this).val()));
            });
            $(".searchclear").toggle(Boolean($(".searchinput").val()));
            $(".searchclear").click(function(){
                $(this).parent().find('.searchinput').val('').focus();
                $(this).hide();
            });
        });

Essentially what it does is add scope to the clear buttons so that it is limited to the sibling. 本质上,它的作用是将范围添加到清除按钮,以便将其限制在同级按钮上。 There are other jQuery functions that might be more specific, but this should work. 还有其他jQuery函数可能更具体,但这应该可以工作。

http://www.bootply.com/130368 http://www.bootply.com/130368

Another option would be to use .siblings() to make sure you are targeting just the siblings with the searchclear class. 另一种选择是使用.siblings()来确保您仅针对具有searchclear类的同级searchclear

        $(document).ready(function(){
            $(".searchinput").keyup(function(){
                $(this).siblings(".searchclear").toggle(Boolean($(this).val()));
            });
            $(".searchclear").toggle(Boolean($(".searchinput").val()));
            $(".searchclear").click(function(){
                $(".searchinput").val('').focus();
                $(this).hide();
            });
        });

http://www.bootply.com/130369 http://www.bootply.com/130369

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM