我在aspx站点上执行javascript函数时遇到问题,该函数应该在TextBox(MultiLine)内容的开头插入一个String,并将Cursor设置在插入的字符串的中间。 我根本就没有接到电话。
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>
插入的String应如何显示:
13.11.2012 / 13:08 / LightMonk
----------------------------------------
(Position of the Cursor here!)
----------------------------------------
我用于此的来源:
Set keyboard caret position in html textbox
更正了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>
答案 0 :(得分:2)
首先,您的代码应如下所示,在document.ready()上使用处理程序注册(我删除了asp.net标记) http://jsfiddle.net/rMmwq/
请注意
var len = wert.length;
而不是
var len = wert.length();