当HTML渲染maxlength属性不显示? [英] maxlength attribute doesn't display when html is rendered?

查看:411
本文介绍了当HTML渲染maxlength属性不显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框的文本模式设置为多行。这样做可以使文本框为textarea的。东西这样的效果:

I have a textbox whose textmode is set to multiline. Doing this renders the textbox as a textarea. Something to this effect:

< ASP:文本框ID =txtFinBillingTermsMAXLENGTH =500=服务器工具提示=(例如90/10,30/30/30/10等)的TextMode =多行栏目=5行=5WIDTH =300像素>< / ASP:文本框>

我遇到的问题是,当我跑我的项目和检查textarea的渲染HTML不显示maxlength属性,其作为如果去了:

The issue I am having is when I run my project and inspect the textarea the rendered html does not show the maxlength attribute, its as if its gone:

呈现的HTML:

<textarea name="ctl00$MainContent$txtFinBillingTerms" rows="5" cols="5" id="MainContent_txtFinBillingTerms" title="(e.g. 90/10, 30/30/30/10, etc.)" style="width:300px;"></textarea>

这导致问题对我来说,因为我试图把一些使用Javascript / jQuery来限制我的textarea的输入,即这样的:

This causes issues for me because I am trying to put in some javascript/jquery to limit input of my textarea, namely this:

$('textarea').keypress(function (e) {
            var maxLength = $(this).attr('maxlength');
            alert(maxLength);
            if (e.which < 0x20) {
                // e.which < 0x20, then it's not a printable character
                // e.which === 0 - Not a character
                return;     // Do nothing
            }
            if (this.value.length == max) {
                e.preventDefault();
            } else if (this.value.length > max) {
                // Maximum exceeded
                this.value = this.value.substring(0, max);
            }
        });

不过最大长度始终是未定义....

But maxLength is always undefined....

推荐答案

最大长度属性不适用于&LT; textarea的&GT; ,所以它完全忽略。从<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.maxlength%28v=vs.110%29.aspx\"相对=nofollow> MSDN :

MaxLength property isn't applicable for <textarea> so it's simply ignored. From MSDN:

仅在TextMode属性设置为TextBoxMode.SingleLine或TextBoxMode.Password该属性适用。

This property is applicable only when the TextMode property is set to TextBoxMode.SingleLine or TextBoxMode.Password.

小写最大长度属性适用于&LT; textarea的&GT; 但不是所有浏览器都支持它,所以它可能取决于或ASP.NET将如何处理该属性(它不支持它,所以它可能只是删除该属性)。

Lowercase maxlength attribute works on <textarea> but not all browsers supports it so it may depends on that or how ASP.NET will handle that property (it doesn't support it so it may simply remove that attribute).

编辑::您可以使用一些JavaScript或与该前pression验证控件解除此限制: ValidationEx pression =^ [\\ S \\ S] {} 0,500 $ http://forums.asp.net/t/40065.aspx?TextBox%20MultiLine%20maxlength($ C $从的这里)。如果你只想执行客户端验证,那么你至少有下列选项:

You can workaround this limitation using some JavaScript or a validation control with this expression: ValidationExpression="^[\s\S]{0,500}$" (code from here). If you want to perform only client-side validation then you have at least these options:

1)使用不同的名称)

1) Use a different name for that attribute (like data-maxlength) both in your ASP.NET page and in your JavaScript:

<asp:TextBox runat="server" ID="txtFinBillingTerms"
    data-maxlength="500"
    ToolTip="(e.g. 90/10, 30/30/30/10, etc.)"
    TextMode="MultiLine"
    Columns="5" Rows="5" Width="300px">
</asp:TextBox>

使用:

var maxLength = $(this).data('maxlength');

2)如果你不直接使用你的&LT; ASP:输入&GT; 在ASP.NET code控制,那么你可以使用&LT; textarea的=服务器&GT; 代替的官方的ASP.NET控件,像的这个例子

2) If you don't use directly your <asp:Input> control in your ASP.NET code then you may use a <textarea runat="server"> instead of official ASP.NET control, like this example:

<textarea runat="server" ID="txtFinBillingTerms"
    maxlength="500"
    title="(e.g. 90/10, 30/30/30/10, etc.)"
    cols="5" rows="5" style="width: 300px">
</textarea>

这篇关于当HTML渲染maxlength属性不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆