Javascript sending key codes to a <textarea>

2019-01-26 01:09发布

I can't figure out how to trigger a keydown event on a textarea element, e.g. imagine i have two textarea elements and when i type something in the first one, I want the second box to show the typing as well, but for a certain reason I have to do it via events. This is what I tried and it doesn't work:

<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>

<textarea id='first'></textarea>
<textarea id='second'></textarea>

<script>
    jQuery("#first").keydown(function(event)
    {
        var keydown = jQuery.Event("keydown")
        keydown.keyCode = event.keyCode
        keydown.which = event.which
        jQuery("#second").trigger(keydown)
    })
</script>

Any ideas how could I achieve that?

5条回答
何必那么认真
2楼-- · 2019-01-26 01:23

The event is sent, the browser just doesn't react to it (i.e., put characters in the textarea)

Not all browsers (that I know of) allow you to dispatch events, not just trigger them. But even doing that is far from perfect

// Gecko only
$("#first").keypress(function(event)
{
  var evt = document.createEvent('KeyEvents');
  evt.initKeyEvent(
      event.type
    , event.bubbles
    , event.cancelable
    , event.view
    , event.ctrlKey
    , event.altKey
    , event.shiftKey
    , event.metaKey
    , event.keycode
    , event.charCode
  );
  $('#second')[0].dispatchEvent( evt );
});

Try this example and you'll see what I mean by how it's far from perfect. Basically, the way to do what you're asking is also the way you say you can't - by value.

However, you can pass custom data along with the event, which leads to a solution that looks like this

$("#first").bind( 'keyup change', function(event)
{
  $('#second').trigger( 'mimic', $(this).val() );
});
$("#second").bind( 'mimic', function( event, value )
{
  $(this).val( value );
})

Is that good enough?

查看更多
你好瞎i
3楼-- · 2019-01-26 01:27

Here's an example: apparently you add to the textarea's value property.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-26 01:30

You can't just "create" events on the client's machine. Can't you imagine the potential security vulnerabilities? It's a no-brainer. The only thing you can do is fire an event handler...

textarea2.onkeydown();
查看更多
可以哭但决不认输i
5楼-- · 2019-01-26 01:33

Try this:

$(document).ready(function() {
var $comment = '';
$('#first').keyup(function() {
    $comment = $(this).val();
    $comment = $comment.replace(/<\/?[^>]+>/g,"").replace(/\n/g, "<br />").replace(/\n\n+/g, '<br /><br />'); // this strips tags and then replaces new lines with breaks
    $('#second').html( $comment );
});
});

Working demo: http://jsbin.com/ivulu

Or if you don't want to sanitize the data: http://jsbin.com/oposu

$(document).ready(function() {
var $comment = '';
$('#first').keyup(function() {
    $comment = $(this).val();
    $('#second').html( $comment );
});
});
查看更多
Lonely孤独者°
6楼-- · 2019-01-26 01:35

The keydown event is not responsible for actually updating the value of the textbox. Your code works just fine, but since you don't have an event handler on the second textbox, it doesn't look like anything happens. If you want to update the text in the textbox, change its value, as demonstrated by jyoseph.

If you need to trigger the event as well (for some reason), your code as written will do that.

查看更多
登录 后发表回答