我有一个分数表,显示了许多学生的许多分数。当教师浏览表格时,程序应选择字段内的文本,以便教师可以更改分数而不必删除先前的值。
这是我目前的咖啡脚本:
$("input:text").focus ->
$(this).select()
if $('#scoreTable').length > 0
document.getElementById("r0c0").focus()
$(".some_cell").keydown (e) ->
if e.which == 38
next_row = parseInt($(this).attr("cell_row")) - 1
next_col = parseInt($(this).attr("cell_col"))
else if e.which == 40
next_row = parseInt($(this).attr("cell_row")) + 1
next_col = parseInt($(this).attr("cell_col"))
else if e.which == 37
next_row = parseInt($(this).attr("cell_row"))
next_col = parseInt($(this).attr("cell_col")) - 1
else if e.which == 39
next_row = parseInt($(this).attr("cell_row"))
next_col = parseInt($(this).attr("cell_col")) + 1
$("#r"+next_row+"c"+next_col).focus()
初始选择命令在页面加载时正常工作。但是当我用箭头键跳转到另一个单元格时,光标会放在文本之后。没有选择任何内容。
这里是创建表的嵌入式 ruby html:
<div>
<table class="table-borderless scoreTable" id="scoreTable">
<thead class="table-borderless">
<th class="table-borderless">Student</th>
<% @objectives.each_with_index do |objective, index| %>
<th class="rotate table-borderless"><div><span>
<%= link_to objective.short_name, edit_objective_path(objective) %>
</span></div></th>
<% end %>
</thead>
<tbody>
<% @students.each_with_index do |student, indexx| %>
<tr>
<td><h4><%= link_to student.last_name_first, edit_teaching_requests_student_url(student) %> </h4></td>
<% this_ss = student.seminar_students.find_by(:seminar_id => @seminar.id) %>
<% @objectives.each_with_index do |objective, indexy| %>
<% this_score = @scores.find_by(:objective => objective, :user => student) %>
<td class="themScores to_unhide score_cell">
<% if this_score %>
<%= text_field "scores[#{this_score.id}]", "", :class => "some_cell", :value => this_score.points,
:id => "r#{indexx}c#{indexy}", :cell_row => indexx, :cell_col => indexy %>
<% end %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</div>
我尝试将此行添加到咖啡脚本:
$("#r"+next_row+"c"+next_col).select()
我已经尝试调整这个问题的建议寻找一个更好的解决方法来 Chrome select on focus bug,我认为这会转化为这样的咖啡脚本:
$("#r"+next_row+"c"+next_col).select().mouseup (e) ->
e.preventDefault()
$(this).unbind("mouseup")
提前感谢您的任何见解。