当asp.net文本区域处于只读模式时,如何禁用ENTER键

How to disable ENTER key when a asp.net textarea is in readonly mode

本文关键字:模式 ENTER 何禁用 只读 文本 net asp 区域 于只读      更新时间:2023-09-26

ASP.net:

<textarea id="taskNotes" runat="server" class="taskNotes" rows="10" style=""></textarea>

HTML生成的ASP.net文本区域:

<textarea name="ctl00$ContentMain$taskNotes" class="taskNotes" id="ContentMain_taskNotes" style="" rows="10" readOnly="readonly"/>

当文本区域具有焦点和只读时,如何禁用ENTER键执行。

我尝试了以下内容,但未能完成:

$('input[class=taskNotes]').keydown(function (e) {
    if (('.taskNotes')) { // '.is()` is not populating in VS for me to complete...
        if (e.keyCode === 13) {
            e.preventDefault();
            return false;
        }
    }
});

试试这个:

使用此选项可防止输入密钥

$(document).ready(function () {
    $(document).on('keydown', '.taskNotes[readonly]', function(e){
        if (e.which === 13) {
            e.preventDefault();
            return false;
        }
    });
});

或者作为一种替代方案,使用它来完全防止元素的焦点:

$(document).ready(function () {    
    $(document).on('focus', '.taskNotes[readonly]', function(e){
        $(this).blur();
    });
});
$('.taskNotes').keydown(function (e) {    
    if ($(this).attr('readonly') === 'readonly' && e.keyCode === 13) {
        e.preventDefault();
        return false;
    }
});

您需要从选择器中删除"input"。这是一个文本区域。jsFiddle

要添加只读属性,可以使用:

$('.taskNotes').prop("readonly", true);

$('.taskNotes').attr("readonly", "readonly");

取决于您的jQuery版本。

如果使用ASP.Net,则需要使用TextBox服务器控件。

Disabled TextBox生成Disabled而不是readonly。它更适合您的场景。

您的操作方式在ASP.Net中不是标准的,而且非常脆弱。您可以读取"禁用"和只读控件。

<asp:Panel ID="Panel1" runat="server" DefaultButton="SubmitButton">
    <asp:TextBox ID="MyTextArea" TextMode="multiline"
        Columns="50" Rows="5" runat="server" />
    <asp:Button runat="server" ID="SubmitButton" Text="Submit"
        OnClick="SubmitButton_Click" />
</asp:Panel>
protected void Page_Load(object sender, EventArgs e)
{
    MyTextArea.Enabled = false;
}