日期:2014-05-16  浏览次数:20413 次

如何在打开页面时将光标定位在某个文本框?
<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);" />