admin管理员组文章数量:1026989
I am trying to achieve AutoFocus functionality on the first empty form field which can be an input element or any kind i.e.
1. textbox
2. radio button
3. Select
4. checkbox
I have had a look at the jquery website and tried using the :input selector but I am unable to achieve Autofocus on any input field other than text box. I have also setup a JS Fiddle,
Fiddle for code
Can somebody help here?
I am trying to achieve AutoFocus functionality on the first empty form field which can be an input element or any kind i.e.
1. textbox
2. radio button
3. Select
4. checkbox
I have had a look at the jquery website and tried using the :input selector but I am unable to achieve Autofocus on any input field other than text box. I have also setup a JS Fiddle,
Fiddle for code
Can somebody help here?
Share Improve this question edited Oct 5, 2015 at 13:44 Sunil 92915 silver badges25 bronze badges asked Oct 5, 2015 at 12:59 CSharpedCSharped 1,2974 gold badges21 silver badges53 bronze badges 1- Did you even read the question, if that makes sense? – Praveen Kumar Purushothaman Commented Oct 5, 2015 at 13:02
2 Answers
Reset to default 6You might need to use filter()
to find the inputs with empty values:
$(function () {
$("input:enabled").filter(function () {
return this.value.trim() == "";
}).first().focus();
});
This will not work if you are using checkbox
. So for that:
$(function () {
$("input:enabled").filter(function () {
if ($(this).attr("type") == "checkbox" || $(this).attr("type") == "radio")
return $(this).val().trim() == "" || !this.checked;
else
return this.value.trim() == "";
}).first().focus();
});
Fiddle: http://jsfiddle/7fwrd2rk/
You need to filter()
out the empty/unchecked inputs and set focus to the first element in the matched set, as below.
$(function () {
$("input:enabled").filter(function () {
return this.type.match(/checkbox|radio/i) && //when it's a checkbox/radio
!this.checked || //get me a list of unselected ones
($.trim(this.value) === ""); // else get me a list of empty inputs
}).first().focus(); //grab the first from the list and set focus
});
Here is a demo along the same lines.
This includes textarea and select.
I am trying to achieve AutoFocus functionality on the first empty form field which can be an input element or any kind i.e.
1. textbox
2. radio button
3. Select
4. checkbox
I have had a look at the jquery website and tried using the :input selector but I am unable to achieve Autofocus on any input field other than text box. I have also setup a JS Fiddle,
Fiddle for code
Can somebody help here?
I am trying to achieve AutoFocus functionality on the first empty form field which can be an input element or any kind i.e.
1. textbox
2. radio button
3. Select
4. checkbox
I have had a look at the jquery website and tried using the :input selector but I am unable to achieve Autofocus on any input field other than text box. I have also setup a JS Fiddle,
Fiddle for code
Can somebody help here?
Share Improve this question edited Oct 5, 2015 at 13:44 Sunil 92915 silver badges25 bronze badges asked Oct 5, 2015 at 12:59 CSharpedCSharped 1,2974 gold badges21 silver badges53 bronze badges 1- Did you even read the question, if that makes sense? – Praveen Kumar Purushothaman Commented Oct 5, 2015 at 13:02
2 Answers
Reset to default 6You might need to use filter()
to find the inputs with empty values:
$(function () {
$("input:enabled").filter(function () {
return this.value.trim() == "";
}).first().focus();
});
This will not work if you are using checkbox
. So for that:
$(function () {
$("input:enabled").filter(function () {
if ($(this).attr("type") == "checkbox" || $(this).attr("type") == "radio")
return $(this).val().trim() == "" || !this.checked;
else
return this.value.trim() == "";
}).first().focus();
});
Fiddle: http://jsfiddle/7fwrd2rk/
You need to filter()
out the empty/unchecked inputs and set focus to the first element in the matched set, as below.
$(function () {
$("input:enabled").filter(function () {
return this.type.match(/checkbox|radio/i) && //when it's a checkbox/radio
!this.checked || //get me a list of unselected ones
($.trim(this.value) === ""); // else get me a list of empty inputs
}).first().focus(); //grab the first from the list and set focus
});
Here is a demo along the same lines.
This includes textarea and select.
本文标签: javascriptAutofocus on the first empty field in the formStack Overflow
版权声明:本文标题:javascript - Autofocus on the first empty field in the form - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745664379a2162089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论