读书人

怎么在打开页面时将光标定位在某个文本

发布时间: 2012-09-04 14:19:30 作者: rapoo

如何在打开页面时将光标定位在某个文本框?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr align="right" style="height:15px;">
<td align="center"><%# DataBinder.Eval(Container.DataItem, "ptdtmx_xh")%></td>
<td>
<%# string.Format("{0:N"+iXXW+"}", ((Double)(DataBinder.Eval(Container.DataItem, "ptdtmx_je"))))%>
</td>
<td align="right" style="padding-right:5px;">
<input id="txtJE" name="txtJE" maxlength="15" pp="tt" type="text" value="<%# DataBinder.Eval(Container.DataItem, "ptdtmx_je") %>" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

输入框是通过Repeater产生的多个,数量不定,我通过:

$(document).ready(function () {
$("input[pp='tt']")[0].focus();
});

这样定位焦点,为什么不起作用呢?

[解决办法]
$("input[id='txtJE']:first").focus();

或是
$("input[id='txtJE']").eq(0).focus();

[解决办法]
$(function(){
$("input[name='txtJE']:first").focus();

});
[解决办法]

HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title> New Document </title>  <meta name="Generator" content="EditPlus">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content=""> </head><script type="text/javascript">    function start(){        var firstInput=document.getElementsByTagName("input")[0];        firstInput.select();    }</script> <body onload="start();"><label>text1:</label>  <input type="text" /><br/><label>text2:</label>  <input type="text" /><br/><label>text3:</label>  <input type="text" /><br/><label>text4:</label>  <input type="text" /><br/> </body></html>
[解决办法]
<input id="txtJE" name="txtJE" maxlength="15" pp="tt" type="text" t_value="" o_value=""
svalue="<%# DataBinder.Eval(Container.DataItem, "ptdtmx_je") %>" ivalue="<%# DataBinder.Eval(Container.DataItem, "ptdtmx_srje") %>" isright="<%# DataBinder.Eval(Container.DataItem, "ptdtmx_df") %>" te=""
style="width:150px; font-size:12px; padding-top:3px;border:0px none white;height:15px; line-height:15px; text-align:right; background-color:#CCE8CF;"
value='<%# ((double)(DataBinder.Eval(Container.DataItem, "ptdtmx_srje"))==0)?"":string.Format("{0:N"+iXXW+"}", ((Double)(DataBinder.Eval(Container.DataItem, "ptdtmx_srje"))))%>'
onKeyPress="fkeyPress(this);" onkeyup="fkeyUp(this);" onfocus="fonFocus(this);" onblur="fonBlur(this);" />



大家的思路都给你了,单例都可以使用,用到你那不行了,你就应该自己去找问题在哪里。而不是把问题丢回来给别人,坐享其成。检查你自己的函数去吧。我只是本着有始有终的原则再次回答你这个问题。拒绝这种不劳而获的人
或许:$("input[name='txtJE']:first").select();可以实现你的效果,你的fonFocus中setSelection在我这里无法执行

读书人网 >JavaScript

热点推荐