输入文本中的JQuery移动和ASCII字符

I have this problem in my web application developed using jQuery Mobile / PHP

  • The application is intended to be used on tablet like iPad, by tablet browser

  • inside my application I have a form built in jQuery mobile with some text and one text area fields

  • In the text area field I need to insert also special chars that normally on desktop pc I would write using Alt+ASCII code (for example Alt+157, Alt+225, Alt+0177)

  • On iPad I didn't find a way to click Alt+ASCII code on virtual tablet, so I'm thinking to add to my form some buttons, one for each special chars I need, that when clicked pass to the text area the Alt+ASCII code combination. Is it possible? If yes, how is it possible?

  • when the form will be submitted then the content of the fields will be stored to the Mysql db... but this is not an issue

thank you very much, Matt

I had a similar problem sometime back. I don't think you can rely on simulating keypress (which is what I think you're trying to do). keypress will not happen if its not trusted and specifying what key to simulating might not work on some browsers. But, here's how that's achieved.

$("button").click( function() {
  var e = jQuery.Event("keydown");
  e.which = 50; 
  $("textarea").trigger(e);
});

But, this doesn't work on Chrome. So i suggest you try injecting the value through code. I setup a fiddle for you for this current situation. Here's the markup

 <textarea></textarea>
 <button class="special" data-char="~">Insert tilde</button>

and the JS

$(".special").click(function () {
  var char = $(this).data("char");
  $("textarea").val(function () {
      return this.value + char;
  });

});

and the demo http://jsfiddle.net/hungerpain/8qs2G/