gpt4 book ai didi

javascript 有条件地禁用按钮

转载 作者:行者123 更新时间:2023-12-02 17:53:14 27 4
gpt4 key购买 nike

为什么我收到警报,但在从选择更新总计后将负值更改为正值后按钮未启用。这是不起作用的部分:

    if ($('.grand_total').val() < 0) {
$('#submit').prop('disabled', true);
alert('negative number found');

} else if ($('.grand_total').val() > 0) {
$('#submit').prop('disabled', false);
alert('positive number found');
}

这是完整的代码:

 <script language="javascript">
$(".add_to_total").on('change', function() {
var total = 0;
var grand_total = 0;
$(".dynamic_row").each(function() {
var row = $(this);

var start_hour_am = parseFloat(row.find(".start_hour_am").val()) || 0;
var start_minute_am = parseFloat(row.find(".start_minute_am").val()) || 0;

var end_hour_am = parseFloat(row.find(".end_hour_am").val()) || 0;
var end_minute_am = parseFloat(row.find(".end_minute_am").val()) || 0;

var start_hour_pm = parseFloat(row.find(".start_hour_pm").val()) || 0;
var start_minute_pm = parseFloat(row.find(".start_minute_pm").val()) || 0;

var end_hour_pm = parseFloat(row.find(".end_hour_pm").val()) || 0;
var end_minute_pm = parseFloat(row.find(".end_minute_pm").val()) || 0;

total = ( (Number(end_hour_am) + (Number(end_minute_am))) - (Number(start_hour_am) + Number(start_minute_am)) + (Number(end_hour_pm) + Number(end_minute_pm)) - (Number(start_hour_pm) + Number(start_minute_pm)));
row.find(".total").val(total);
grand_total = Number(grand_total) + Number(total);

});
$("#grand_total").val(grand_total);


if ($('.grand_total').val() < 0) {
$('#submit').prop('disabled', true);
alert('negative number found');

} else if ($('.grand_total').val() > 0) {
$('#submit').prop('disabled', false);
alert('positive number found');
}

});
</script>

任何想法将不胜感激。

更新这是总计的 html:

<input type="text" class="grand_total" name="grand_total" id="grand_total" data-role="none" value="0" size="3" readonly="true">

这是我试图禁用的按钮:

<button type="submit" data-theme="e" data-mini="true" data-inline="true" name="submit" id="submit" class="submit" data-icon="check" value="submit-value">Submit</button>

按照 Nick N 的建议,按钮禁用/启用仍然遇到同样的问题。

<script language="javascript">
$(".add_to_total").on('change', function() {
var total = 0;
var grand_total = 0;
$(".dynamic_row").each(function() {
var row = $(this);

var start_hour_am = parseFloat(row.find(".start_hour_am").val()) || 0;
var start_minute_am = parseFloat(row.find(".start_minute_am").val()) || 0;

var end_hour_am = parseFloat(row.find(".end_hour_am").val()) || 0;
var end_minute_am = parseFloat(row.find(".end_minute_am").val()) || 0;

var start_hour_pm = parseFloat(row.find(".start_hour_pm").val()) || 0;
var start_minute_pm = parseFloat(row.find(".start_minute_pm").val()) || 0;

var end_hour_pm = parseFloat(row.find(".end_hour_pm").val()) || 0;
var end_minute_pm = parseFloat(row.find(".end_minute_pm").val()) || 0;

total = ( (Number(end_hour_am) + (Number(end_minute_am))) - (Number(start_hour_am) + Number(start_minute_am)) + (Number(end_hour_pm) + Number(end_minute_pm)) - (Number(start_hour_pm) + Number(start_minute_pm)));
row.find(".total").val(total);
grand_total = Number(grand_total) + Number(total);

});
$("#grand_total").val(grand_total);


//if (parseFloat($('.grand_total').val()) < 0) {
// $('#submit').prop('disabled', true);
// alert('negative number found');

//} else if (parseFloat($('.grand_total').val()) > 0) {
// $('#submit').prop('disabled', false);
// alert('positive number found');
//}


var total = parseFloat($('#grand_total').val());
if(total < 0){
$('#submit').prop('disabled', true);
alert('negative number found...');
}
else {
$('#submit').prop('disabled', false);
alert('positive number found...');
}

});
</script>

更新好吧,看起来问题是因为该按钮是 jquery mobile 生成的按钮,当发现负值时,它不会更新按钮的状态,如果我刷新整个表单,按钮状态就会改变。我通过将数据 Angular 色设置为无来测试这一点,以便提交按钮成为标准表单按钮并且禁用/启用功能有效。我有什么想法可以解决这个问题吗?

最佳答案

这应该有效:

var total = parseFloat($('.grand_total').val());
if(total < 0){
$('#submit').attr("disabled", "disabled");
alert('negative number found');
}
else {
$('#submit').removeAttr("disabled");
alert('positive number found');
}
<小时/>

Jquery Mobile:不要刷新整个表单,而只刷新按钮:

$('#submit').button('refresh');
<小时/>

请注意:我将“#”更改为“.”。根据您的 HTML,您还可以更改此行:

 $(".grand_total").val(grand_total);

至:

$("#grand_total").val(grand_total);

关于javascript 有条件地禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21185614/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com