简体   繁体   English

如果在jQuery中放错了位置,脚本将不起作用

[英]Script doesn't work if misplaced in jQuery

i will create website with validate in input field, i used Mottie Keyboard in every input field, and i use validation on it. 我将在输入字段中创建带有验证的网站,在每个输入字段中都使用Mottie键盘,并在其上使用验证。 i will create disable button when validation is not correct. 验证不正确时,我将创建禁用按钮。 And I get a script for it, directly from github page mottie keyboard. 我直接从github页面mottie键盘上获得了一个脚本。 I want is if the validation was not correct then the button in a virtual keyboard can not be pressed. 我想要的是,如果验证不正确,则无法按下虚拟键盘中的按钮。 Here's the script: 这是脚本:

var toggleKeysIfEmpty = function( kb ) {
             var toggle = kb.$preview.val() === '';
             console.log( toggle, kb.$preview.val() );
             kb.$keyboard
        .find('.ui-keyboard-bksp')
        .toggleClass('disabled', toggle)
        .prop('disabled', toggle);
};

And this my script before adding script above: 这是我的脚本,然后在上面添加脚本:

$(function() {

  // change default navigation keys    
  $('#jkeyboard2, #jkeyboard').keyboard({
    layout: 'num',
    // setting alwaysOpen does odd things with input focus on initialization
    // best to leave it false and focus on the desired input
    // alwaysOpen: true,
    autoAccept: true,
    usePreview: false,
    position: {
      of: $(window),
      // null (attach to input/textarea) or a jQuery object (attach elsewhere)
      my: 'center bottom',
      at: 'center bottom',
      at2: 'center bottom'
    },
    maxLength: 4,
    layout: 'custom',
    customLayout: {
      'normal': ['1 2 3', '4 5 6', '7 8 9', '0 . {b}'],
    },
    visible : function(e, keyboard) {
        toggleKeysIfEmpty( keyboard );
    },
    tabNavigation: true,
    initialFocus: false,
    initialized: function() {
        setTimeout(function(){
            $('#jkeyboard').focus();
        }, 200);
    },
    change: function(e, keyboard, el) {
      if (keyboard.$el.val().length >= 4) {
        // switchInput( goToNext, isAccepted );
        keyboard.switchInput(true, true);
      } else if (keyboard.$el.val() === "" && keyboard.last.key === "bksp") {
        // go to previous if user hits backspace on an empty input
        keyboard.switchInput(false, true);
      }
    }
  })
});

$(document).ready(function() {
      $('#jkeyboard').bind('keyboardChange', function (e, keyboard, el) {
        if (validatePhone('jkeyboard')) {
          $('#spnPhoneStatus').html('');
          $('#spnPhoneStatus').css('color', 'green');
        } else {
          $('#spnPhoneStatus').html('<b>Wrong Number</b>');
          $('#spnPhoneStatus').css('color', 'red');
        }
      });
    });

    function validatePhone(jkeyboard) {
      var a = document.getElementById(jkeyboard).value;
      var filter = /^0(?:8(?:(?:1(?:[789][0-9]{0,9})?|3(?:[1238][0-9]{0,9})?|5(?:9[0-9]{0,9})?|7(?:[78][0-9]{0,9})?)?)?)?$/;
      //var filter = /^0([8]([1357]([123789]([0-9]{0,8}))?)?)?$/;
      if (filter.test(a)) {
        return true;
      } else {
        return false;
      }
    }

I want disable backspace button if validation was not correct, so i added the script. 如果验证不正确,我想禁用退格按钮,因此我添加了脚本。 And become like this: 变成这样:

$(function() {

  // change default navigation keys    
  $('#jkeyboard2, #jkeyboard').keyboard({
    layout: 'num',
    // setting alwaysOpen does odd things with input focus on initialization
    // best to leave it false and focus on the desired input
    // alwaysOpen: true,
    autoAccept: true,
    usePreview: false,
    position: {
      of: $(window),
      // null (attach to input/textarea) or a jQuery object (attach elsewhere)
      my: 'center bottom',
      at: 'center bottom',
      at2: 'center bottom'
    },
    maxLength: 4,
    layout: 'custom',
    customLayout: {
      'normal': ['1 2 3', '4 5 6', '7 8 9', '0 . {b}'],
    },
    visible : function(e, keyboard) {
        toggleKeysIfEmpty( keyboard );
    },
    tabNavigation: true,
    initialFocus: false,
    initialized: function() {
        setTimeout(function(){
            $('#jkeyboard').focus();
        }, 200);
    },
    change: function(e, keyboard, el) {
      if (keyboard.$el.val().length >= 4) {
        // switchInput( goToNext, isAccepted );
        keyboard.switchInput(true, true);
      } else if (keyboard.$el.val() === "" && keyboard.last.key === "bksp") {
        // go to previous if user hits backspace on an empty input
        keyboard.switchInput(false, true);
      }
    }
  })
});

$(document).ready(function() {
      $('#jkeyboard').bind('keyboardChange', function (e, keyboard, el) {
        if (validatePhone('jkeyboard')) {
          $('#spnPhoneStatus').html('');
          $('#spnPhoneStatus').css('color', 'green');
        } else {
          $('#spnPhoneStatus').html('<b>Wrong Number</b>');
          $('#spnPhoneStatus').css('color', 'red');
      var toggleKeysIfEmpty = function( kb ) {
             var toggle = kb.$preview.val() === '';
             console.log( toggle, kb.$preview.val() );
             kb.$keyboard
        .find('.ui-keyboard-bksp')
        .toggleClass('disabled', toggle)
        .prop('disabled', toggle);
            };
        }
      });
    });

    function validatePhone(jkeyboard) {
      var a = document.getElementById(jkeyboard).value;
      var filter = /^0(?:8(?:(?:1(?:[789][0-9]{0,9})?|3(?:[1238][0-9]{0,9})?|5(?:9[0-9]{0,9})?|7(?:[78][0-9]{0,9})?)?)?)?$/;
      //var filter = /^0([8]([1357]([123789]([0-9]{0,8}))?)?)?$/;
      if (filter.test(a)) {
        return true;
      } else {
        return false;
      }
    }

it's doesn't work, and i try put the code i get at the top of my code. 它不起作用,我尝试将我得到的代码放在代码的顶部。 it's work but backspace button can not be pressed from the start. 它是可行的,但是不能从一开始就按退格键。 Anyone can fix it? 任何人都可以修复它吗?

Here's my fiddle: DEMO 这是我的小提琴: 演示

I have changed your code to update the fiddle here : you wanted to 我更改了您的代码以更新此处小提琴 :您想

disable backspace button if validation was not correct 如果验证不正确,请禁用退格按钮

so here's what I did: 所以这就是我所做的:

  1. I renamed toggleKeysIfEmpty to toggleBackspaceKey and changed its implementation to add the CSS classes to render the button correctly depending on the desired state: 我改名toggleKeysIfEmptytoggleBackspaceKey ,改变它的实现添加CSS类正确取决于所期望的状态来呈现按钮:

    var toggleBackspaceKey = function( kb, toggle ) {
    console.log( toggle, kb.$preview.val() ); var $bkSpaceBtn = kb.$keyboard.find('.ui-keyboard-bksp'); if (toggle) { $bkSpaceBtn .attr({ 'disabled': 'disabled', 'aria-disabled': 'true' }) .removeClass(kb.options.css.buttonHover) .addClass(kb.options.css.buttonDisabled); } else { $bkSpaceBtn .removeAttr('disabled') .attr({ 'aria-disabled': 'false' }) .addClass(kb.options.css.buttonDefault) .removeClass(kb.options.css.buttonDisabled); } };

  2. I changed the implementation of the bksp keyaction handler to ensure that if it's invoked when the button is disabled, no action is executed. 我更改了bksp keyaction处理程序的实现,以确保如果在禁用按钮时调用了它,则不会执行任何操作。 The handler for the backspace will be invoked if you press the corresponding key or if you double click on the backspace button in the keyboard even when it's disabled (this might be a bug). 如果按下相应的键或双击键盘中的退格按钮(即使已禁用),也会调用退格处理程序(这可能是一个错误)。 Here is the handler code: if the backspace button is enabled it simply invokes the default backspace processing handler. 这是处理程序代码:如果启用了退格按钮,则仅调用默认的退格处理程序。 Also, this function is invoked once from the visible callback: 同样,此函数从visible回调中调用一次:

    var processBkSpcKey = function(kb) { var originalBkSpaceHandler = $.keyboard.keyaction.bksp; $.keyboard.keyaction.bksp = function(base) { // If the backspace button is disabled, do not process it. var $bkSpaceBtn = kb.$keyboard.find('.ui-keyboard-bksp'); if($bkSpaceBtn.hasClass(kb.options.css.buttonDisabled)) { return false; }
    return originalBkSpaceHandler.apply(kb, arguments); } };

With these changes in place, the backspace button is disabled if the input is empty or if the validation fails, in this case though, how would the user clear the contents of the input? 进行这些更改后,如果输入为空或验证失败,则将禁用退格按钮,但是在这种情况下,用户将如何清除输入的内容?

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

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