简体   繁体   English

如何使输入字段末尾的 fontAwesome Icon 也可点击

[英]How to make fontAwesome Icon at the end of the input field clickable too

I have an input field with jQuery date picker.我有一个带有 jQ​​uery 日期选择器的输入字段。 So when you click on the input field it will show the calendar.因此,当您单击输入字段时,它将显示日历。 I have also inserted a calendar Font Awesome icon in the :after.我还在 :after 中插入了一个日历 Font Awesome 图标。 How do I make that part clickable too and launch the jQuery calendar?如何使该部分也可点击并启动 jQuery 日历? I can't make the background of the input field transparent because I need it to be white.我无法使输入字段的背景透明,因为我需要它是白色的。

 $( function() { $( "#dateOfBirth" ).datepicker({ numberOfMonths: 2, dateFormat: 'dd-mm-yy', maxDate: '0', minDate: '-18M' }); });
 .addingCalendarIcon:after{ content: "\\f073"; color:black; } .addingCalendar:after{ position:relative; right:260px; top:5px; float:right; font-family: FontAwesome; }
 <link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div> <label>Date of Birth : </label> <div> <span class="addingCalendar addingCalendarIcon"> <input type="search" placeholder="dd-mm-yyyy" name="dateOfBirth" onchange="dateOfBirthCheck();adding28Days();makingSixWeekDate();making40dayDate();" id="dateOfBirth" readonly="true"/> </span> </div> </div>

You can just make the input element focused when the calendar icon is clicked.您可以在单击日历图标时使输入元素成为焦点。

$( function() {
    $( "#dateOfBirth" ).datepicker({
        numberOfMonths: 2,
        dateFormat: 'dd-mm-yy',
        maxDate: '0',
        minDate: '-18M'
    });   
    $('.addingCalendar').on("click", function(e){
    console.log("clicked");
        $('#dateOfBirth').focus();
    });
  } );

JSFiddle Demo JSFiddle 演示

Try something as like below snippet.尝试类似下面的代码片段。 Instead of adding fontawesome icon in after pseudo position it and write click event using jquery.而不是after伪定位after添加 fontawesome 图标并使用 jquery 编写click事件。

 $('#calendarIcon').click(function() { alert('calendar icon clicked'); });
 #calendarIcon { position: relative; z-index: 1; left: -25px; top: 1px; color: #7B7B7B; cursor: pointer; width: 0; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" /> <input id="calendar" type="text" placeholder="DD/MM/YYYY" /> <i id="calendarIcon" class="fa fa-calendar"></i>

Its not possible to make the pseudo Elements clickable as they are not part of the DOM.不可能使伪元素可点击,因为它们不是 DOM 的一部分。

What you can do in your case is make the element containing the input field( .addingCalendar ) trigger the datepicker instead of ( #dateOfBirth ).在您的情况下,您可以做的是使包含输入字段( .addingCalendar )的元素触发日期选择器而不是( #dateOfBirth )。

$( ".addingCalendar" ).datepicker({
        numberOfMonths: 2,
        dateFormat: 'dd-mm-yy',
        maxDate: '0',
        minDate: '-18M'

    });   

created a fiddle: https://jsfiddle.net/zg45anab/创建了一个小提琴: https : //jsfiddle.net/zg45anab/

Use separate element for icon and wrap the input and icon element with parent wrapper为图标使用单独的元素并使用父包装器包装输入和图标元素

HTML HTML

 <div>
        <label>Date of Birth : </label>
        <div class="input_wrapper">
             <input type="search" placeholder="dd-mm-yyyy" name="dateOfBirth" onchange="dateOfBirthCheck();adding28Days();makingSixWeekDate();making40dayDate();"  id="dateOfBirth" readonly="true"/>
            <span class="addingCalendar addingCalendarIcon fa fa-calendar"></span>
        </div>
    </div>

CSS CSS

    .input_wrapper{
         position: relative;
    }
    .addingCalendarIcon{
         position: absolute;
         right:0;
         top:0;
         width: 30px;
         height: 30px;
    }

jQuery jQuery

    $( ".addingCalendarIcon" ).click(function(){
        $( "#dateOfBirth" ).trigger("focus");
    });   

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

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