简体   繁体   中英

How disable javascript effect on separated input field?

This is the Javascript that turns on Georgian keyboard in every input and textarea field

But I have one input field with ID user_login which type is text and of course, this javascript takes effect on this field too I simply want to disable the effect of this javascript only for this field which ID is user_login

Please help me thank you in advance

HTML

<!DOCTYPE html>
    <html>
    <head>
<title></title>
<link rel="stylesheet" type="text/css" href="jquery.geokbd.css">
<script type="text/javascript" src="/js/jquery_min.js"></script>
<script type="text/javascript" src="jquery.geokbd.js"></script>
    </head>
    <body>

<div class="gk-switcher">
<input id="kbd-switcher" type="checkbox">
</div>

<form>
    <input type="text" placeholder="Title">
    <input type="text" placeholder="Description">
    <input placeholder="Password" type="password">
    <input placeholder="Email" type="email">
</form>

    <input placeholder="Email" type="email">
    <input placeholder="About me" maxlength="11" type="text">
    <input placeholder="Username:" type="text" name="user_login" id="user_login" class="wide">

    <script>
$('#kbd-switcher').geokbd();
    </script>

    </body>
    </html>

and code

(function($, undefined) {

$.fn.geokbd = function(options) {
    var 
    isOn, 
    inputs = $([]),
    switchers = $([]),
    defaults = {
        on: true,
        hotkey: '`'
    },
    settings = (typeof options === 'object' ? $.extend({}, defaults, options) : defaults);

    // first come up with affected set of input elements
    this.each(function() {
        var $this = $(this);

        if ($this.is(':text, textarea')) {
            inputs = inputs.add($this);
        } else if ($this.is('form')) {
            inputs = inputs.add($this.find(':text, textarea'));
        } else if ($this.is(':checkbox')) {
            if (!inputs.length) {
                inputs = $(':text, textarea');
            }
            switchers = switchers.add($this); // store the checkboxes for further manipulation
        }

        if (typeof settings.exclude === 'string') {
            inputs = inputs.not(settings.exclude);
        }
    });

    // mutate switchers
    switchers
        .click(function() { toggleLang() })
        .wrap('<div class="gk-switcher"></div>')
        .parent()
            .append('<div class="gk-ka" /><div class="gk-us" />');

    // turn on/off all switchers
    toggleLang(isOn = settings.on);

    $(document).keypress(function(e) {
        var ch = String.fromCharCode(e.which), kach;

        if (settings.hotkey === ch) {
            toggleLang();
            e.preventDefault();
        }

        if (!isOn || !inputs.filter(e.target).length) {
            return;
        }

        kach = translateToKa.call(ch);

        if (ch != kach) {
            if (navigator.appName.indexOf("Internet Explorer")!=-1) {
                window.event.keyCode = kach.charCodeAt(0);
            } else {
                pasteTo.call(kach, e.target);
                e.preventDefault();
            }
        }
    });

    function toggleLang() {
        isOn = arguments[0] !== undefined ? arguments[0] : !isOn;
        switchers
            .each(function() {
                this.checked = isOn;
            })
            .closest('.gk-switcher')[isOn ? 'addClass' : 'removeClass']('gk-on');
    }

    // the following functions come directly from Ioseb Dzmanashvili's GeoKBD (https://github.com/ioseb/geokbd)

    function translateToKa() {
        /**
        * Original idea by Irakli Nadareishvili
        * http://www.sapikhvno.org/viewtopic.php?t=47&postdays=0&postorder=asc&start=10
        */
        var index, chr, text = [], symbols = "abgdevzTiklmnopJrstufqRySCcZwWxjh";

        for (var i = 0; i < this.length; i++) {
            chr = this.substr(i, 1);
            if ((index = symbols.indexOf(chr)) >= 0) {
                text.push(String.fromCharCode(index + 4304));
            } else {
                text.push(chr);
            }
        }
        return text.join('');
    }

    function pasteTo(field) {
        field.focus();
        if (document.selection) {
            var range = document.selection.createRange();
            if (range) {
                range.text = this;
            }
        } else if (field.selectionStart != undefined) {
            var scroll = field.scrollTop, start = field.selectionStart, end = field.selectionEnd;
            var value = field.value.substr(0, start) + this + field.value.substr(end, field.value.length);
            field.value = value;
            field.scrollTop = scroll;
            field.setSelectionRange(start + this.length, start + this.length); 
        } else {
            field.value += this;
            field.setSelectionRange(field.value.length, field.value.length);    
        }
    };
}

}(jQuery));

If you call your plugin on the form element, instead of the checkbox and then search the document for all desired element types except the one with the id of user_login , your inputs JQuery wrapper will only contain the elements you want.

 (function($, undefined) { $.fn.geokbd = function(options) { var isOn, inputs = $([]), switchers = $([]), defaults = { on: true, hotkey: '`' }, settings = (typeof options === 'object' ? $.extend({}, defaults, options) : defaults); // first come up with affected set of input elements // Using the standard DOM API, search the document for all `input` and // 'textarea' elements, except for the one with an id of: "user_login" document.querySelectorAll("input:not(#user_login), textarea").forEach(function(item) { var $this = $(item); // You had the selector for an input incorrect if ($this.is('input[type="text"], textarea')) { inputs = inputs.add($this); } else if ($this.is('form')) { inputs = inputs.add($this.find('input[type="text"], textarea')); } else if ($this.is(':checkbox')) { if (!inputs.length) { inputs = $('input[type="text"], textarea'); } switchers = switchers.add($this); // store the checkboxes for further manipulation } if (typeof settings.exclude === 'string') { inputs = inputs.not(settings.exclude); } }); // mutate switchers switchers .click(function() { toggleLang() }) .wrap('<div class="gk-switcher"></div>') .parent() .append('<div class="gk-ka" /><div class="gk-us" />'); // turn on/off all switchers toggleLang(isOn = settings.on); $(document).keypress(function(e) { var ch = String.fromCharCode(e.which), kach; if (settings.hotkey === ch) { toggleLang(); e.preventDefault(); } if (!isOn || !inputs.filter(e.target).length) { return; } kach = translateToKa.call(ch); if (ch != kach) { if (navigator.appName.indexOf("Internet Explorer")!=-1) { window.event.keyCode = kach.charCodeAt(0); } else { pasteTo.call(kach, e.target); e.preventDefault(); } } }); function toggleLang() { isOn = arguments[0] !== undefined ? arguments[0] : !isOn; switchers .each(function() { this.checked = isOn; }) .closest('.gk-switcher')[isOn ? 'addClass' : 'removeClass']('gk-on'); } // the following functions come directly from Ioseb Dzmanashvili's GeoKBD (https://github.com/ioseb/geokbd) function translateToKa() { /** * Original idea by Irakli Nadareishvili * http://www.sapikhvno.org/viewtopic.php?t=47&postdays=0&postorder=asc&start=10 */ var index, chr, text = [], symbols = "abgdevzTiklmnopJrstufqRySCcZwWxjh"; for (var i = 0; i < this.length; i++) { chr = this.substr(i, 1); if ((index = symbols.indexOf(chr)) >= 0) { text.push(String.fromCharCode(index + 4304)); } else { text.push(chr); } } return text.join(''); } function pasteTo(field) { field.focus(); if (document.selection) { var range = document.selection.createRange(); if (range) { range.text = this; } } else if (field.selectionStart != undefined) { var scroll = field.scrollTop, start = field.selectionStart, end = field.selectionEnd; var value = field.value.substr(0, start) + this + field.value.substr(end, field.value.length); field.value = value; field.scrollTop = scroll; field.setSelectionRange(start + this.length, start + this.length); } else { field.value += this; field.setSelectionRange(field.value.length, field.value.length); } }; } $('form').geokbd(); }(jQuery));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="jquery.geokbd.css"> </head> <body> <div class="gk-switcher"> <input id="kbd-switcher" type="checkbox"> </div> <form> <input type="text" placeholder="Title"> <input type="text" placeholder="Description"> <input placeholder="Password" type="password"> <input placeholder="Email" type="email"> </form> <input placeholder="Email" type="email"> <input placeholder="About me" maxlength="11" type="text"> <input placeholder="Username:" type="text" name="user_login" id="user_login" class="wide"> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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