检查表单字段是否在页面加载时有值,如果有,显示标签?
Check if form fields have values on page load and if so, show labels?
我有一个表单,在用户向该字段添加值后,每个字段上方都会显示小标签。
此 for 有时会加载一些预填充的字段。
如果任何表单字段有值,我将如何检查页面加载?如果有,标签是否可见?
这是我当前用于在字段具有值后显示标签的代码:
$('.form-control').blur(function() {
if( $(this).val() ) {
$(this).prev().show();
}
});
在页面加载时试试这个:
$('.form-control').each(function() {
if( $(this).val() ) {
$(this).prev().show();
}
});
$(document).ready(function(){
$('.form-control').each(function(){
if($(this).val() != ''){
$(this).prev().show();
}
});
});
准备好文档后,对于每个 .form-control,如果输入的值不为空,请执行您要显示标签的任何代码。
使用 focusout
事件不会太过分,对吗?
<script type="text/javascript">
(function ($) {
$(document).ready(function (e) {
// ALTHOUGH BLUR IS OK, ONE COULD SIMPLY BIND THE .form-control CLASSES TO THE focusout EVENT
// THIS ENSURES THAT THERE IS A HIGHER LIKELIHOOD THAT THE FIELD IN QUESTION ONCE HAD FOCUS
// WHICH MAY IMPLY THAT THE USER ALSO INTERACTED WITH THE FIELD IN SOME WAY...
$('.form-control').each(function(elem){
var objElem = $(this);
objElem.focusout(function(evt) {
if ($(this).val()) {
// NOW, YOU SHOULD KNOW WHICH METHOD TO USE TO TRAVERSE THE DOM
// AND GET AT THE LABEL....
// IN YOUR CASE IT SEEMS TO BE THE PREVIOUS ELEMENT BEFORE THE FORM-FIELD.
$(this).prev().show();
}
});
});
});
})(jQuery);
</script>
我有一个表单,在用户向该字段添加值后,每个字段上方都会显示小标签。
此 for 有时会加载一些预填充的字段。
如果任何表单字段有值,我将如何检查页面加载?如果有,标签是否可见?
这是我当前用于在字段具有值后显示标签的代码:
$('.form-control').blur(function() {
if( $(this).val() ) {
$(this).prev().show();
}
});
在页面加载时试试这个:
$('.form-control').each(function() {
if( $(this).val() ) {
$(this).prev().show();
}
});
$(document).ready(function(){
$('.form-control').each(function(){
if($(this).val() != ''){
$(this).prev().show();
}
});
});
准备好文档后,对于每个 .form-control,如果输入的值不为空,请执行您要显示标签的任何代码。
使用 focusout
事件不会太过分,对吗?
<script type="text/javascript">
(function ($) {
$(document).ready(function (e) {
// ALTHOUGH BLUR IS OK, ONE COULD SIMPLY BIND THE .form-control CLASSES TO THE focusout EVENT
// THIS ENSURES THAT THERE IS A HIGHER LIKELIHOOD THAT THE FIELD IN QUESTION ONCE HAD FOCUS
// WHICH MAY IMPLY THAT THE USER ALSO INTERACTED WITH THE FIELD IN SOME WAY...
$('.form-control').each(function(elem){
var objElem = $(this);
objElem.focusout(function(evt) {
if ($(this).val()) {
// NOW, YOU SHOULD KNOW WHICH METHOD TO USE TO TRAVERSE THE DOM
// AND GET AT THE LABEL....
// IN YOUR CASE IT SEEMS TO BE THE PREVIOUS ELEMENT BEFORE THE FORM-FIELD.
$(this).prev().show();
}
});
});
});
})(jQuery);
</script>