1

私は編集を許可する2つのモードを持つフォームを持っています(テキストボックスの値)

and another mode for (non-manager) user - code behind adding "READONLY" to the TextBox tag.

so with the readonly 'mode' there's no input accepted in text box at all

my question is how could i execute validation method based on "READONLY" property of text box ?

strRo = "READONLY" on a condition

<asp:TextBox ID="txtNumbers" runat="server" <%=strRo %> onkeypress="return allowonlynumbers();" />

javascript function

   function allowonlynumbers() {
        if (event.keyCode >= 48 && event.keyCode <= 57) {
            return true;
            }

        else {
            alert('Only numbers can be entered');
             return false;
        }

    }
4

2 に答える 2

3

関数呼び出しで要素への参照を渡す必要があるため、 を次のように変更しonkeypressます。

onkeypress="return allowonlynumbers(this);"

次に、関数は次のようにする必要があります。

function allowonlynumbers(obj) {

}

使用できるロジックは次のとおりです。

if (obj.readOnly) {
    // The element is readonly
}
于 2012-09-24T16:09:51.680 に答える
2

イベントハンドラーをjavascriptでバインドする必要があります(なぜですか?)。次にthis、ハンドラー内の演算子を使用してテキストボックスを参照できるようになります。テキストボックスは次のreadOnlyプロパティを確認できます。

document.getElementById('test').onkeypress = function () {
    if (!this.readOnly) {
        if (event.keyCode >= 48 && event.keyCode <= 57) {
            return true;
        } else {
            alert('Only numbers can be entered');
            return false;
        }
    }
};​

http://jsfiddle.net/FedsQ/

于 2012-09-24T17:20:52.930 に答える