我想检查所有输入是否都具有值,而不是删除按钮上的Disabled属性。
在下面,我插入了一个代码段, 但这会在只有一个输入具有值时删除该属性。
$(".follow-url-inputs").each(function(index, item) {
$(item).change(function() {
var empty = $(this).filter(function() {
return this.value === "";
});
if (empty.length) {
console.log("all fiels filled in");
} else {
$('.fs_btn-save').removeAttr('disabled')
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="follow-inputs text-center px-5">
<div class="input-group px-5">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fab fa-facebook"></i>
</span>
</div>
<input id="facebook" class="form-control follow-url-inputs" name="facebook" placeholder="https://facebook.com/followURL" data-com.agilebits.onepassword.user-edited="yes">
</div><br>
<div class="input-group px-5">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fab fa-linkedin"></i>
</span>
</div>
<input id="linkedin" class="form-control follow-url-inputs" name="linkedin" placeholder="https://linkedin.com/followURL" data-com.agilebits.onepassword.user-edited="yes">
</div><br>
<div class="input-group px-5">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fab fa-twitter"></i>
</span>
</div>
<input id="twitter" class="form-control follow-url-inputs" name="twitter" placeholder="https://twitter.com/followURL" data-com.agilebits.onepassword.user-edited="yes">
</div><br></div>
<div class="text-center">
<button class="btn btn-primary fs_btn-primary fs_btn-save" disabled onclick="setupFollowButtons()">Save</button>
</div>
有人可以帮助我检查所有输入是否都具有值,然后删除禁用的属性。
答案 0 :(得分:2)
逻辑有点倒退。您要过滤每个更改内部的所有输入。您当前的代码仅过滤更改的代码
var $inputs = $(".follow-url-inputs").change(function() {
var inValid = $inputs.filter(function() {
return !this.value.trim();
}).length;
$('.fs_btn-save').prop('disabled', inValid)
if (!inValid) {
console.log("all fiels filled in");
}
});
答案 1 :(得分:0)
使用jquery映射方法。这将提供一个对象,如果任何字段为空,它将具有一个键,该键的值将为true
。
执行Object.values
时,它将返回所有值的数组,并检查其是否具有true
,这意味着该字段中的任何一个为空,在这种情况下,按钮状态可以取消
还将change
更改为keyup
$(".follow-url-inputs").keyup(function() {
let m = $(".follow-url-inputs").map(function(item) {
return $(this).val() === ""
})
// map will give an object
// if any input is empty it will contain true
if (Object.values(m).indexOf(true) !== -1) {
console.log("all fiels filled in");
} else {
$('.fs_btn-save').removeAttr('disabled')
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="follow-inputs text-center px-5">
<div class="input-group px-5">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fab fa-facebook"></i>
</span>
</div>
<input id="facebook" class="form-control follow-url-inputs" name="facebook" placeholder="https://facebook.com/followURL" data-com.agilebits.onepassword.user-edited="yes">
</div><br>
<div class="input-group px-5">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fab fa-linkedin"></i>
</span>
</div>
<input id="linkedin" class="form-control follow-url-inputs" name="linkedin" placeholder="https://linkedin.com/followURL" data-com.agilebits.onepassword.user-edited="yes">
</div><br>
<div class="input-group px-5">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fab fa-twitter"></i>
</span>
</div>
<input id="twitter" class="form-control follow-url-inputs" name="twitter" placeholder="https://twitter.com/followURL" data-com.agilebits.onepassword.user-edited="yes">
</div><br></div>
<div class="text-center">
<button class="btn btn-primary fs_btn-primary fs_btn-save" disabled onclick="setupFollowButtons()">Save</button>
</div>
答案 2 :(得分:0)
试试看
$(".follow-url-inputs").each(function(index, item) {
$(item).change(function() {
var isEmpty = true;
$('input').each(function() {
if ($.trim($(this).val()) == '') {
isEmpty = true;
}
else {
isEmpty = false;
}
});
if (isEmpty) {
console.log("isAllEmpty: "+ isEmpty);
} else {
$('.fs_btn-save').removeAttr('disabled')
console.log("isAllEmpty: "+ isEmpty);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="follow-inputs text-center px-5">
<div class="input-group px-5">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fab fa-facebook"></i>
</span>
</div>
<input id="facebook" class="form-control follow-url-inputs" name="facebook" placeholder="https://facebook.com/followURL" data-com.agilebits.onepassword.user-edited="yes">
</div><br>
<div class="input-group px-5">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fab fa-linkedin"></i>
</span>
</div>
<input id="linkedin" class="form-control follow-url-inputs" name="linkedin" placeholder="https://linkedin.com/followURL" data-com.agilebits.onepassword.user-edited="yes">
</div><br>
<div class="input-group px-5">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fab fa-twitter"></i>
</span>
</div>
<input id="twitter" class="form-control follow-url-inputs" name="twitter" placeholder="https://twitter.com/followURL" data-com.agilebits.onepassword.user-edited="yes">
</div><br></div>
<div class="text-center">
<button class="btn btn-primary fs_btn-primary fs_btn-save" disabled onclick="setupFollowButtons()">Save</button>
</div>