1

我在 aspx 站点上执行 javascript 函数时遇到问题,该函数应该在 TextBox(多行)内容的开头插入一个字符串,并将光标设置在插入的字符串的中间。它根本不会在我这边被调用。

ASPX 代码:

    <head runat="server">
    <title>Unbenannte Seite</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function setCaretPosition(elemId, caretPos) {
            var elem = document.getElementById(elemId);

            if(elem != null) {
                if(elem.createTextRange) {
                    var range = elem.createTextRange();
                    range.move('character', caretPos);
                    range.select();
                }
                else {
                    if(elem.selectionStart) {
                        elem.focus();
                        elem.setSelectionRange(caretPos, caretPos);
                    }
                    else
                        elem.focus();
                }
            }
        }

        $("#<%=Button1.ClientID %>").click(function(e){
            e.preventDefault();
            var d = new Date();
            var curr_date = d.getDate();
            var curr_month = d.getMonth() + 1;
            var curr_year = d.getFullYear();
            var curr_hour = d.getHours();
            var curr_minute = d.getMinutes();
            var name = "Name";
            var wert = curr_date + "." + curr_month + "." + curr_year + " / " + curr_hour + ":" + curr_minute + " / " + name + "\n" + "----------------------------------------" + "\n";
            var len = wert.length();
            wert = wert + "\n" + "----------------------------------------" + "\n" + "\n";
            var input = $("#<%=tbVZusatz.ClientID %>");
            input.val(wert + input.val());
            input.val(input.val());
            setCaretPosition("<%=tbVZusatz.ClientID %>",len);
        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="tbVZusatz" runat="server" TextMode="MultiLine" Height="500px" Width="300px"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Stempel" />
    </div>
    </form>
</body>

插入的字符串应如下所示:

13.11.2012 / 13:08 / LightMonk
----------------------------------------
(Position of the Cursor here!)
---------------------------------------- 

我用于此的来源:

http://jsfiddle.net/VP9zT/1/

在 html 文本框中设置键盘插入符号位置

更正的Javascript:

<script type="text/javascript">
    $(document).ready(function(){
        $("#<%=Button1.ClientID %>").click(function(e){
            e.preventDefault();
            var d = new Date();
            var curr_date = d.getDate();
            var curr_month = d.getMonth() + 1;
            var curr_year = d.getFullYear();
            var curr_hour = d.getHours();
            var curr_minute = d.getMinutes();
            var name = "Name";
            var wert = curr_date + "." + curr_month + "." + curr_year + " / " + curr_hour + ":" + curr_minute + " / " + name + "\n" + "----------------------------------------" + "\n";
            var len = wert.length;
            wert = wert + "\n" + "----------------------------------------" + "\n" + "\n";
            var input = $("#<%=tbVZusatz.ClientID %>");
            input.val(wert + input.val());
            input.val(input.val());
            setCaretPosition("<%=tbVZusatz.ClientID %>",len);
        });
    });

    function setCaretPosition(elemId, caretPos) {
        var elem = document.getElementById(elemId);

        if(elem != null) {
            if(elem.createTextRange) {
                var range = elem.createTextRange();
                range.move('character', caretPos);
                range.select();
            }
            else {
                if(elem.selectionStart) {
                    elem.focus();
                    elem.setSelectionRange(caretPos, caretPos);
                }
                else
                    elem.focus();
            }
        }
    }


</script>
4

1 回答 1

2

首先,您的代码应如下所示,在 document.ready() 上注册处理程序(我删除了 asp.net 标记) http://jsfiddle.net/rMmwq/

另请注意

 var len = wert.length;

代替

 var len = wert.length();
于 2012-11-13T13:37:54.770 回答