Jquery 每行禁用一个按钮
这里有一个脚本,它获取所有行输入的总和。如果to Quantity_total字段为空或等于零,则需要禁用每行添加按钮。这是我现在拥有的 任何帮助都将不胜感激Jquery 每行禁用一个按钮,jquery,Jquery,这里有一个脚本,它获取所有行输入的总和。如果to Quantity_total字段为空或等于零,则需要禁用每行添加按钮。这是我现在拥有的 任何帮助都将不胜感激 $('.qtys').blur(function () { var sum = 0; $(this).closest('tr').find('.qtys').each(function () { sum += Number($(this).val()); }); $(this).closes
$('.qtys').blur(function () {
var sum = 0;
$(this).closest('tr').find('.qtys').each(function () {
sum += Number($(this).val());
});
$(this).closest('tr').find('.qty_total').val(sum);
});
$('.qty_total').each(function() {
if ($(this).val() != "") {
$('.add_to_cart').prop("disabled",false);
} else {
$('.add_to_cart').prop("disabled",true);
}
});
您可以使用attr()
和removeAttr()
并且您需要将每个循环放入事件中
这样做:
$('.qty_total').each(function() {
if ($(this).val() != "") {
$(this).closest('tr').find('.add_to_cart').removeAttr("disabled");
} else {
$(this).closest('tr').find('.add_to_cart').attr("disabled","disabled");
}
});
$('.qty_total').each(function() {
if ($(this).val() != "") {
$(this).closest('tr').find('.add_to_cart').prop("disabled",false);
} else {
$(this).closest('tr').find('.add_to_cart').prop("disabled",true);
}
});
把这个放在模糊事件里面
或者您可以像这样使用prop()
:
$('.qty_total').each(function() {
if ($(this).val() != "") {
$(this).closest('tr').find('.add_to_cart').removeAttr("disabled");
} else {
$(this).closest('tr').find('.add_to_cart').attr("disabled","disabled");
}
});
$('.qty_total').each(function() {
if ($(this).val() != "") {
$(this).closest('tr').find('.add_to_cart').prop("disabled",false);
} else {
$(this).closest('tr').find('.add_to_cart').prop("disabled",true);
}
});
你是说:
//disable all add buttons on document load
$('.add_to_cart').prop("disabled",true);
$('.qtys').blur(function () {
var sum = 0;
$(this).closest('tr').find('.qtys').each(function () {
sum += Number($(this).val());
});
var qtyEle = $(this).closest('tr').find('.qty_total');
qtyEle.val(sum);
//check if sum exists and enable/disable accordingly
$(this).parents("tr").find(".add_to_cart").prop("disabled", !sum);
});
检查此项
checkTotal();
$('.qtys').blur(function () {
var sum = 0;
$(this).closest('tr').find('.qtys').each(function () {
sum += Number($(this).val());
});
$(this).closest('tr').find('.qty_total').val(sum);
checkTotal();
});
function checkTotal(){
$('.qty_total').each(function() {
if ($(this).val() === "" || $(this).val() === 0 ) {
$(this).closest('tr').find('.add_to_cart').prop("disabled",true);
} else {
$(this).closest('tr').find('.add_to_cart').prop("disabled",false);
}
});
}
$('.add_to_cart').prop("disabled",true);
$('.qtys').blur(function () {
var sum = 0;
var parentTr = $(this).closest('tr');
parentTr.find('.qtys').each(function () {
sum += Number($(this).val());
});
parentTr.find('.qty_total').val(sum);
parentTr.find('.add_to_cart').prop("disabled",(sum === "" || sum === 0));
});
优化版本:
checkTotal();
$('.qtys').blur(function () {
var sum = 0;
$(this).closest('tr').find('.qtys').each(function () {
sum += Number($(this).val());
});
$(this).closest('tr').find('.qty_total').val(sum);
checkTotal();
});
function checkTotal(){
$('.qty_total').each(function() {
if ($(this).val() === "" || $(this).val() === 0 ) {
$(this).closest('tr').find('.add_to_cart').prop("disabled",true);
} else {
$(this).closest('tr').find('.add_to_cart').prop("disabled",false);
}
});
}
$('.add_to_cart').prop("disabled",true);
$('.qtys').blur(function () {
var sum = 0;
var parentTr = $(this).closest('tr');
parentTr.find('.qtys').each(function () {
sum += Number($(this).val());
});
parentTr.find('.qty_total').val(sum);
parentTr.find('.add_to_cart').prop("disabled",(sum === "" || sum === 0));
});
更改:
如果()
条件是错误的,那么它所做的恰恰相反checkTotal()
中值=0添加了条件
$(this).closest('tr').find('.add_To_cart')查找相应的
;而不是$('.add_to_cart')
-这将禁用/启用所有按钮
$('.qty_total')放在代码中。每个代码都在代码中,但您还应该修复一些问题:
$('.qtys').blur(function () {
var sum = 0;
$(this).closest('tr').find('.qtys').each(function () {
sum += Number($(this).val());
});
$(this).closest('tr').find('.qty_total').val(sum);
// put this piece inside.
$('.qty_total').each(function() {
if ($(this).val() != "") {
// prop --> attr and other change.
$(this).closest('tr').find('.add_to_cart').attr("disabled",false);
} else {
// prop --> attr and other change.
$(this).closest('tr').find('.add_to_cart').attr("disabled",true);
}
});
});
您可以在onblur
事件处理程序中禁用按钮,因为如果不这样做,则在重新编辑(使总数再次为空)后,按钮将不会被禁用
$('.qtys').blur(function () {
var sum = 0;
$(this).closest('tr').find('.qtys').each(function () {
sum += Number($(this).val());
});
$(this).closest('tr').find('.qty_total').val(sum)
.closest('td').next()
.find('.add_to_cart')[0].disabled = sum == 0;
}).blur();
如果使用$('.add_to_cart'),JQuery将返回所有元素have class=“add_to_cart” 试试这个,模拟文本框上的更改事件
$('.add_to_cart').prop("disabled",true);
$('.qtys').blur(function () {
var sum = 0;
$(this).closest('tr').find('.qtys').each(function () {
sum += Number($(this).val());
});
$(this).closest('tr').find('.qty_total').val(sum).change();
});
$(document).on('change','.qty_total',function() {
if ($(this).val() != "" || $(this).val() != 0) {
$(this).parent().parent().find('.add_to_cart').prop("disabled",false);
} else {
$(this).parent().parent().find('.add_to_cart').prop("disabled",true);
}
});
FIDLE:为每行设置一个特定的“id”或“名称”!!禁用所有关闭按钮,当每个字段都被更改时,启用具有特定IDI的按钮在这种情况下,您的名字attr很适合这样做!使用它们.attr(“禁用”、“禁用”)代码>存在兼容性问题(chrome版本35.0.1916.114 m)。。使用prop
instead我看不到任何反对票,但是@King Kong你看不到你的ans吗?下面已经发布了其他ans的副本。你做的正是其他人做的posted@Pilot这里所有的答案都是相似的,我的代码完全不同。写作风格,甚至最初使用blur()
(无参数)触发onblur
(目前没有人使用)。我很欣赏你的代码,但我不会对你的综合代码风格投赞成票..+1..我选择了你的优化版本。很好,它帮助了你!