2

I have the following textarea HTML tag that I am inserting a runtime into the DOM (I have only shown how it looks after it is inserted) and I'm set the onkeypress event to fire as an inline attribute. However, in Firefox I get an "event is not defined" error when I press a key went it is in focus.

<textarea rows="3" cols="70" type="text" name="resultsRename" value="" class="redlining_textContent_Dialog_textbox" dojoType="dijit.forms.SimpleTextArea" id="labelText" propercase="false" style="width:auto;" onkeypress="return handleEnterKey(event,doFunc();">

function handleEnterKey(e, func)
{
    //alert("hit key handler");
    //alert(dojo.toJson(e));
    var key = e.keyCode || e.which;
    if (key == 13)
    {
        func();
        return false;
    }
    else
    {
        return true;
    }

}

I have looked at tons of help telling me that I should expect an event object parameter to be available to the js code I place in the onkeypress attr, but I keep getting this error.

The textarea tag is not inside a pair of form tags (I saw somewhere that this can be an issue sometimes but I have no choice here).

I have tried a handleEnterKey function with only the e parameter but this makes no difference.

If I change the inline attribute to...

onkeypress="return handleEnterKey(this,doFunc();"

... I enter the handleEnterKey with my e parameter set to the textarea object. Why Am I not being passed the event object? Obviously this works fine in IE with it's global event object.

At my wits end. Help me Stack Overflow... you're my only hope!

4

1 回答 1

4

handleEnterKey甚至不应调用,因为您有语法错误。

return handleEnterKey(event,doFunc();

应该

return handleEnterKey(event,doFunc);

通过event肯定有效:DEMO

于 2011-10-05T15:37:10.770 回答