我有一个从元素获取值的函数:
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;
}现在我要检查amount、actual_amount和funds_arrival_date是否已填满。如果是,我将从一个按钮上释放被禁用的类。我试过了
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();
});但它似乎不起作用..
发布于 2015-11-30 19:12:12
Remove disabled attribute using JQuery?
你能看看上面的链接吗,我认为我们应该使用$('.inputDisabled').prop("disabled",false);
发布于 2015-11-30 19:11:51
即使单个数组元素为非空,您的代码也会从a中删除类disabled。为了确保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');
}
}或者另一种方式是
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');发布于 2015-11-30 19:13:03
尝试使用Array.prototype.every()
if (options.every(Boolean)) {
$("a[name=review_button]").removeClass("disabled");
} else {
// do other stuff
}https://stackoverflow.com/questions/33997119
复制相似问题