我在浏览器Chrome时遇到一些问题,它会记住密码,用户名等。我无法检查输入字段是否为空。 我使用以下代码,但 if 语句在Chrome中不起作用,而在所有其他浏览器中都有效。
$(document).ready(function() {
$('.inputWithLabel').each(function(){
if ($(this).val().length != 0){
var element_id = $(this).attr('id');
$('label[for="' + element_id + '"]').addClass('inputWithText');
}
});
});
我试图改变以下标签
<form action="" method="post">
<div class="divInputLabel inputLabel">
<label class="labelInput" for="username">Brugernavn*</label>
<input class="inputLabel inputWithLabel" id="username" name="username" type="text">
</div>
<div class="divInputLabel inputLabel">
<label class="labelInput" for="password">Adgangskode*</label>
<input class="inputLabel inputWithLabel" id="password" name="password" type="password">
</div>
<div class="divInputLabel inputLabel">
<label class="labelInput" for="password_again">Gentag adgangskode*</label>
<input class="inputLabel inputWithLabel" id="password_again" name="password_again" type="password">
</div>
<div class="divInputLabel inputLabel">
<label class="labelInput" for="first_name">Fornavn*</label>
<input class="inputLabel inputWithLabel" id="first_name" name="first_name" type="text">
</div>
<div class="divInputLabel inputLabel">
<label class="labelInput" for="last_name">Efternavn</label>
<input class="inputLabel inputWithLabel" id="last_name" name="last_name" type="text">
</div>
<div class="divInputLabel inputLabel">
<label class="labelInput" for="email">Email*</label>
<input class="inputLabel inputWithLabel" id="email" name="email" type="text">
</div>
<div class="divInputLabel inputLabel">
<input type="Submit" class="submit-button" value="Registre">
</div>
</form>
我希望有人可以帮助我。
答案 0 :(得分:1)
要检查并查看输入是否为空,您可以执行更简单的操作。
$(document).ready(function() {
$(".inputWithLabel").change(function() {
if ($(this).val() == null || $(this).val() == "") {
alert("Input is null!");
} else {
alert("Input is valid");
}
});
});
答案 1 :(得分:1)
请勿更改您的代码,而是应用此代码:
$(document).ready(function() {
$('.inputWithLabel').each(function(){
if ($(this).val().length != 0 && $(this).val().length != null){
var element_id = $(this).attr('id');
$('label[for="' + element_id + '"]').addClass('inputWithText');
}
});
});
仅在&& $(this).val().length != null
之后设置$(this).val().length != 0
,它应该在不同的浏览器上运行。试试吧!
答案 2 :(得分:1)
请尝试以下代码:
$(document).ready(function() {
$('body .inputWithLabel').each(function(){
if ($(this).val().length != 0){
var element_id = $(this).attr('id');
$('label[for="' + element_id + '"]').addClass('inputWithText');
}
});
});
只需在每个循环中附加正文$('body .inputWithLabel')
。
答案 3 :(得分:1)
您的代码看起来很好......
Label没有.val()..如果我们想要标签的innertext它应该是.text()
所以用.val()
替换.text()
,它会有希望地工作....
还首先追加身体,即
$('body .inputWithLabel')
答案 4 :(得分:1)
试试这个:
$(document).ready(function() {
$('.inputWithLabel').each(function() {
if ($(this).val()) {
var element_id = $(this).attr('id');
$('label[for="' + element_id + '"]').addClass('inputWithText');
}
});
});
答案 5 :(得分:1)
您可以在自己的网站上试用
$('.inputWithLabel').each(function() {
if ($(this).val() !== null && $(this).val() !== "") {
var element_id = $(this).attr('id');
$("#" + element_id).removeClass('inputWithLabel').addClass('inputWithText');
}
});
答案 6 :(得分:1)
我尝试过类似的事情。您所要做的就是首先将表单ID转换为jQuery变量,
var $elem = document.getElementById("yourformid").value; //make sure your id has no # symbol
if ($elem > 0) {
//do stuff
}
应该有用。
答案 7 :(得分:1)
目前无法检测表单是否由浏览器自动完成填充。我建议的一些替代方案是:
禁用自动填充:如果您不需要它,这将是一个简单的解决方案
<input autocomplete="off">
或整个表格:
<form autocomplete="off">
使用change事件检测字段上的任何更改:
$('#username').on('change keyup',function(){
// to something
});
在这里我可以想象如果输入字段有任何变化就设置一个类,如:
$('#username').on('change keyup',function(){
$(this).addClass('changed');
});
或者更改提交字段的name属性。例如:使用此作为默认值:
<input class="inputLabel inputWithLabel" id="username" name="usernameAutofill" type="text">
如果输入完成了任何内容,请稍后将其更改为
$('#username').on('change keyup',function(){
$(this).attr('name', 'username');
});
但在您做任何此事之前,请先问自己以下问题: 为什么要证明字段? 如果自动填充会发生什么? 如果它们是空的,会发生什么? 如果它们被定期填充会发生什么?
因为什么解决方案最好取决于你想要做什么。如果你回答这个问题,我们可以帮助你更多。
答案 8 :(得分:-2)
尝试使用:
$('').value
或
this.value
或
this.textContent
或
$('').innerHTML
答案 9 :(得分:-2)
您的代码正在检查具有某些值的每个输入元素,而不是那些为空的输入元素。 尝试将您的条件更改为:
if ($(this).val().length == 0){
...
这会将类添加到空字段标签中。