我的网页上有4个文本框和一个提交按钮
假设用户在2个字段中输入数据,然后单击“提交”按钮。
我现在想知道在单击“提交”按钮之前,光标位于哪个文本框中。

关于如何在javascript中执行此操作的任何想法。

最佳答案

您的问题确实在javascript 中明确说了,但是FWIW这是jQuery中的另一种选择:

Working jsFiddle here

HTML:

<input id="in1" type="text" /><br />
<input id="in2" type="text" /><br />
<input id="in3" type="text" /><br />
<input id="in4" type="text" /><br />
<input type="button" id="mybutt" value="Submit" />

jQuery的:
var inFocus = false;

$('input, textarea').focus(function() {
  inFocus = $(this).attr('id');
});

$('#mybutt').click(function() {
    alert('Cursor was last in element id: ' + inFocus);
});

07-24 09:43
查看更多