[英]How to check whether specific items inside an array have a value? Javascript
I have a function which gets values from elements: 我有一个从元素获取值的函数:
function getTransactionValues() {
var o = {};
o.reservations = [];
$('#custom-headers option:selected').each(function (i, selected) {
o.reservations[i] = $(selected).val();
});
o.amount = $('input[name=amount-price]').val();
o.currency_value = $('input[name=currency-value]').val();
o.currency_name = $('.currency_appendto option:selected').html();
o.actual_amount = $('input[name=actual-amount]').val();
o.actual_remaining = $('input[name=actual-remaining]').val();
o.funds_arrival_date = $('input[name=funds-arrival]').val();
o.paid_to = $('.paidto option:selected').html();
o.checkbox = $('.multi-transaction:checked').map(function () {
return this.value
}).get();
return o;
}
Now i want to check whether amount, actual_amount and funds_arrival_date are filled. 现在我要检查金额,actual_amount和funds_arrival_date是否已填满。 if they are i will release the disabled class from a button.
如果他们是我将从一个按钮释放禁用的类。 i've tried
我试过了
var check_review = function () {
var a = getTransactionValues();
var options = [a.amount, a.actual_amount, a.funds_arrival_date];
for(i = 0; i < options.length; i++) {
if(options[i].length > 0) {
$('a[name=review_button]').removeClass('disabled');
}
else{
//this array is empty
alert('There is a problem!');
}
}
}
$('.test').click(function() {
check_review();
});
But it doesn't seems to be working.. 但这似乎不起作用。
Remove disabled attribute using JQuery? 使用JQuery删除禁用的属性?
Can you please look at above link, I think we should use $('.inputDisabled').prop("disabled", false); 您能否看一下上面的链接,我认为我们应该使用$('。inputDisabled')。prop(“ disabled”,false);
Even if a single array element will be non empty then your code will remove the class disabled
from the a
. 即使单个数组元素为非空,您的代码也会从
a
删除disabled
的类。 To make sure that all the elements of array are non empty and then only you want to remove the class then the way is: 为了确保array的所有元素都不为空,然后仅要删除该类,方法是:
for(i = 0; i < options.length; i++) {
if(options[i].length > 0) {
$('a[name=review_button]').removeClass('disabled');
}
else{
$('a[name=review_button]').addClass('disabled');
}
}
Or the other way is 或另一种方式是
var check = true;
for(i = 0; i < options.length; i++) {
if(options[i].length == 0) {
check = false;
}
}
if(check ) $('a[name=review_button]').removeClass('disabled');
Try using Array.prototype.every()
尝试使用
Array.prototype.every()
if (options.every(Boolean)) {
$("a[name=review_button]").removeClass("disabled");
} else {
// do other stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.