Javascript 如何通过动态输入在jQuery中查找百分比

Javascript 如何通过动态输入在jQuery中查找百分比,javascript,html,jquery,Javascript,Html,Jquery,我想找到折扣金额或百分比,两者都很好,但我不能做一点调整 我希望如果我输入10%的0或null,总数应该显示0。目前它将显示10 此外,它还更新了第二次计算中的值,我只希望第二行中的值 $(document).on('change','amountOrPercent',function()){ var amountOrPercent=$(this.find(“:selected”).val(); 如果(amountOrPercent=='amount'){ $(this).closest('.

我想找到折扣金额或百分比,两者都很好,但我不能做一点调整

我希望如果我输入10%的0或null,总数应该显示0。目前它将显示10
此外,它还更新了第二次计算中的值,我只希望第二行中的值

$(document).on('change','amountOrPercent',function()){
var amountOrPercent=$(this.find(“:selected”).val();
如果(amountOrPercent=='amount'){
$(this).closest('.divForCalculation').find('.extraForPercentage').remove();
$(this).closest('.divForCalculation').find('.amountOrPercent').css('padding','4px');
$(this).closest('.divForCalculation').find('.divForCalculation').css('flex','1');
}否则,如果(amountOrPercent==‘百分比’){
$(this).closest('.divForCalculation').find('.divForCalculation').css('flex','2');
$(this).closest('.divForCalculation').find('.amountOrPercent').css('padding','0');
$(this).closest('.divForCalculation').append('Of');
}
});
$(document).on(“change keyup keypress”、“.discountAmount、.totalof”函数(){
var贴现量=0;
var贴现百分比=0;
var totalof=0;
var结果=0;
折扣金额=$(“.discountAmount”).val();
totalof=$(“.totalof”).val();
if(totalof==null | | totalof==0){
totalof=0;
结果=-折扣量;
}
其他的
{  
结果=-parseFloat((parseInt(totalof)/100*parseInt(discountAmount));
}
$('.invoiceItemAmount').val(结果);
});

$
%

全部的
$ %
全部的
你能试试下面的一个吗:

if(totalof === '' || totalof === null || totalof === 0){
    totalof = 0;
    result = 0;
    console.log('result ', result)
} else if(!isNaN(totalof)) {
    
        result = -parseFloat((parseInt(totalof)/100*parseInt(discountAmount)));
}
您可以使用
.closest()
nextAll()
:first
来确定输入目标并更改其中的值。此外,您还可以从“选择框”中获取所选值,具体取决于将此设置为输入框所需的值

演示代码

$(document).on('change','amountOrPercent',function()){
var amountOrPercent=$(this.find(“:selected”).val();
如果(amountOrPercent=='amount'){
$(this).closest('.divForCalculation').find('.extraForPercentage').remove();
$(this).closest('.divForCalculation').find('.amountOrPercent').css('padding','4px');
$(this).closest('.divForCalculation').find('.divForCalculation').css('flex','1');
}否则,如果(amountOrPercent==‘百分比’){
$(this).closest('.divForCalculation').find('.divForCalculation').css('flex','2');
$(this).closest('.divForCalculation').find('.amountOrPercent').css('padding','0');
$(this).closest('.divForCalculation').append('Of');
}
});
$(document).on(“change keyup keypress”、“.discountAmount、.totalof、.amounterpercent”函数(){
变量选择器=$(this.nexist(“.divForCalculation”)//获取最近的div
var贴现量=0;
var贴现百分比=0;
var totalof=0;
var结果=0;
discountAmount=selector.find(“.discountAmount”).val();
totalof=selector.find(“.totalof”).val();
//如果不可见,则添加cond
如果(totalof==null | | totalof==0 | |!selector.find(“.totalof”)。是(“:可见”)){
totalof=0;
//根据选择的值更改值
结果=选择器.find(“.amountOrPercent”).val()=“金额”?-折扣金额:0;
}否则{
结果=-parseFloat((parseInt(totalof)/100*parseInt(discountAmount));
}
//使用nextAll。。
选择器.nextAll('.invoiceItemAmount:first').val(结果);
});

$
%

全部的
$ %
全部的
不工作,因为仅选择$时totalof为NaN Var selector=$(此项)。最近(“.divForCalculation”)$(此项)指的是哪个选择器?因为有三个选择器,所以它指向哪个并不重要,因为使用它我们只需要在该div中获取该
.find
的值就可以了。我已经使用console进行了检查,但这个选择器是.nextAll('.invoiceItemAmount:first').val(result);这不是你原来的html结构吗?对不起,这是我的问题。这不是我原来的html结构