admin管理员组

文章数量:1293383

$(document).ready(function () {
    $("#id").keydown(function () {

    });
})

This code perfectly works for everything(number,alphabet,symbol etc) except Japanese text. On key press it doesn't pass through this event. Does anybody know any solution?

$(document).ready(function () {
    $("#id").keydown(function () {

    });
})

This code perfectly works for everything(number,alphabet,symbol etc) except Japanese text. On key press it doesn't pass through this event. Does anybody know any solution?

Share edited Apr 16, 2013 at 7:00 deceze 523k88 gold badges799 silver badges940 bronze badges asked Apr 16, 2013 at 5:33 sujalsujal 1,0503 gold badges18 silver badges29 bronze badges 12
  • 1 what do you mean by 'Japanese text'? Japanese keyboard layout/input method? – Seth Malaki Commented Apr 16, 2013 at 5:36
  • The solution is to write your own IME in the webpage using JavaScript. This is not going to be easy, but it's actually very do-able. – Pacerier Commented Apr 16, 2013 at 5:41
  • @SethJeremiMalaki yes – sujal Commented Apr 16, 2013 at 5:42
  • @Pacerier I would not remend that. First of all, it's a lot of work. Secondly, users are used to their OS's IME and have possibly customized it, changing that behavior is bad UX. Thirdly, how are you going to replace the OS's IME using your in-browser IME, when the OS IME intercepts keypresses? – deceze Commented Apr 16, 2013 at 5:43
  • puter does not know japanees – Arun Killu Commented Apr 16, 2013 at 5:46
 |  Show 7 more ments

3 Answers 3

Reset to default 6

There's hardly anything you can do. "Japanese text" means an IME, which is a piece of software intercepting the keyboard input and helping you turn it into Japanese text. How this software interacts or doesn't interact with the browser and the browser's Javascript engine depends on the OS, IME, browser and the browser's Javascript engine. On some platforms the keypress is signaled through, in others it isn't. You can try binding to other events like keyup or keypress, some may be signaled even when using an IME.

The best you can do is to make sure you're not depending on keypress events and have fallback options if you can't intercept them; e.g. bind to change events on the text field as well and handle entire text changes, which will be triggered at the end of the IME input.

I got the same issue, instead of prevent user input the text i set the input value to null because the api event.preventDefault(); is not working correctly.

$(document).ready(function () {
    $("#id").keyup(function () {
        this.value = ''
    });
})

I had the same issue and I solved it using the input event.

//calculate the length of a character
function getLen(str){
    var result = 0;
    for(var i=0;i<str.length;i++){
      var chr = str.charCodeAt(i);
      if((chr >= 0x00 && chr < 0x81) ||
         (chr === 0xf8f0) ||
         (chr >= 0xff61 && chr < 0xffa0) ||
         (chr >= 0xf8f1 && chr < 0xf8f4)){
        //half width counted as 1
        result += 1;
      }else{
        //full width counted as 2
        result += 2;
      }
    }
    return result;
};

// trim the string by processing character by character
function getTrimmedString(theString, maxLength){
var tempLength = 0;
var trimmedString = "";
for (var i = 0; i < theString.length; i++) {
    tempLength = getLen(theString.charAt(i)) + tempLength;
    if(tempLength > maxLength){
        break;
    }else{
        trimmedString = trimmedString + theString.charAt(i);
    }
}
return trimmedString;
}

// limit the size of a field
function limitCity(){
var maxChars = 30;
var cityVal = $("#city").val();
var cityLength = getLen(cityVal);
if (cityLength >= maxChars) {
    var trimmedString = getTrimmedString(cityVal, maxChars);
    $("#city").val(trimmedString);
}
}

//bind the input event 
$("#city").bind("input", limitCity);

本文标签: javascriptKeypress event for Japanese textStack Overflow