Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用函数更改复选框上的div内容单击“不工作”_Javascript_Php_Html_Css_Forms - Fatal编程技术网

Javascript 使用函数更改复选框上的div内容单击“不工作”

Javascript 使用函数更改复选框上的div内容单击“不工作”,javascript,php,html,css,forms,Javascript,Php,Html,Css,Forms,如果()语句导致它停止工作,则会出现更多相同的问题 我正在为我的广告代理建立一个自定义订单,用户从表单复选框中选择他们想要的项目,下面的小计/订单汇总表也相应地更新。客户需要获得至少1125美元的基本点,而四个次要点是可选的,为450美元。如果客户决定获得额外的[s],那么贩运成本也会上升(每个$75) 在我达到最高数量之前,一切都正常工作,然后表就不能正常更新 更新了完整问题的处理方法: 更新脚本: if ( totalFinal == "3,330"

如果()语句导致它停止工作,则会出现更多相同的问题

我正在为我的广告代理建立一个自定义订单,用户从表单复选框中选择他们想要的项目,下面的小计/订单汇总表也相应地更新。客户需要获得至少1125美元的基本点,而四个次要点是可选的,为450美元。如果客户决定获得额外的[s],那么贩运成本也会上升(每个$75)

在我达到最高数量之前,一切都正常工作,然后表就不能正常更新

更新了完整问题的处理方法:

更新脚本:

                    if ( totalFinal == "3,330" ) {
                        $('#companion').css("display","block");
                        $('#quantityCompanion').text("4");
                        $('#subTotalCompanion').text("$1,800");
                        $('#quantityTrafficking').text("5");
                        $('#subTotalTrafficking').text("$375");
                    } else if ( totalFinal == "2,775" ) {
                        $('#companion').css("display","block");
                        $('#quantityCompanion').text("3");
                        $('#subTotalCompanion').text("$1,350");
                        $('#quantityTrafficking').text("4");
                        $('#subTotalTrafficking').text("$300");
                    } else if ( totalFinal == "2,250" ) {
                        $('#companion').css("display","block");
                        $('#quantityCompanion').text("2");
                        $('#subTotalCompanion').text("$900");
                        $('#quantityTrafficking').text("3");
                        $('#subTotalTrafficking').text("$225");
                    } else if ( totalFinal == "1,725" ) {
                        $('#companion').css("display","block");
                        $('#quantityCompanion').text("1");
                        $('#subTotalCompanion').text("$450");
                        $('#quantityTrafficking').text("2");
                        $('#subTotalTrafficking').text("$150");
                    } else {
                        $('#companion').css("display","none");
                        $('#quantityTrafficking').text("1");
                        $('#subTotalTrafficking').text("$75");
                    } 

您的
贩运
始终停留在
2
/
/
$80
,因为您的
if
/
else
块将其设置为该值

                if ( totalFinal == "805" ) {
                    $('#companion').css("display","block");
                    $('#quantityTrafficking').text("2");  // Sets to 2 
                    $('#subTotalTrafficking').text("$80");  // Sets to $80
                } else {
                    $('#companion').css("display","none");
                    $('#quantityTrafficking').text("1");  // Ignored due to other else
                    $('#subTotalTrafficking').text("$40"); // Ignored due to other else
                }
                if ( totalFinal == "1,095" ) {
                    $('#companion').css("display","block");
                    $('#quantityCompanion').text("2");
                    $('#subTotalCompanion').text("$500");
                    $('#quantityTrafficking').text("3");  // Sets to 3
                    $('#subTotalTrafficking').text("$120"); // Sets to $120

                } else {
                    $('#quantityTrafficking').text("2"); // Sets to 2, as it overwrites other else
                    $('#subTotalTrafficking').text("$80"); // Sets to $80, as it overwrites other else
                    $('#quantityCompanion').text("1");
                    $('#subTotalCompanion').text("$250");
                }
                if ( totalFinal == "1,095" ) {
                    $('#companion').css("display","block");
                    $('#quantityCompanion').text("2");
                    $('#subTotalCompanion').text("$500");
                    $('#quantityTrafficking').text("3");
                    $('#subTotalTrafficking').text("$120");
                } else if ( totalFinal == "805" ) {
                    $('#companion').css("display","block");
                    $('#quantityCompanion').text("1");
                    $('#subTotalCompanion').text("$250");
                    $('#quantityTrafficking').text("2");
                    $('#subTotalTrafficking').text("$80");
                } else {
                    $('#companion').css("display","none");
                    $('#quantityTrafficking').text("1");
                    $('#subTotalTrafficking').text("$40");
                }
您需要的是一个
if
/
elseif
/
else

                if ( totalFinal == "805" ) {
                    $('#companion').css("display","block");
                    $('#quantityTrafficking').text("2");  // Sets to 2 
                    $('#subTotalTrafficking').text("$80");  // Sets to $80
                } else {
                    $('#companion').css("display","none");
                    $('#quantityTrafficking').text("1");  // Ignored due to other else
                    $('#subTotalTrafficking').text("$40"); // Ignored due to other else
                }
                if ( totalFinal == "1,095" ) {
                    $('#companion').css("display","block");
                    $('#quantityCompanion').text("2");
                    $('#subTotalCompanion').text("$500");
                    $('#quantityTrafficking').text("3");  // Sets to 3
                    $('#subTotalTrafficking').text("$120"); // Sets to $120

                } else {
                    $('#quantityTrafficking').text("2"); // Sets to 2, as it overwrites other else
                    $('#subTotalTrafficking').text("$80"); // Sets to $80, as it overwrites other else
                    $('#quantityCompanion').text("1");
                    $('#subTotalCompanion').text("$250");
                }
                if ( totalFinal == "1,095" ) {
                    $('#companion').css("display","block");
                    $('#quantityCompanion').text("2");
                    $('#subTotalCompanion').text("$500");
                    $('#quantityTrafficking').text("3");
                    $('#subTotalTrafficking').text("$120");
                } else if ( totalFinal == "805" ) {
                    $('#companion').css("display","block");
                    $('#quantityCompanion').text("1");
                    $('#subTotalCompanion').text("$250");
                    $('#quantityTrafficking').text("2");
                    $('#subTotalTrafficking').text("$80");
                } else {
                    $('#companion').css("display","none");
                    $('#quantityTrafficking').text("1");
                    $('#subTotalTrafficking').text("$40");
                }

反复调用
.getElementsByClassName()
是不好的-为什么不直接使用
$('.final')。每个(…)
?。我不明白到底是什么问题。@Pointy我不知道。GetElementsByCassName()不是通过类调用某个对象的最佳方式,谢谢您提供的信息。谢谢你修好小提琴,出于好奇,它怎么了?问题发生在向订单添加现货后,您尝试删除现货,订单摘要中的贩运数量和小计不会分别返回到“1”和“$40”,而是停留在“2”和“$80”。那么
.getElementsByClassName()
,但是浏览器必须处理它,所以每次循环都要重新处理,只为了得到一个元素,效率非常低。既然您已经在使用jQuery,那么不妨全力以赴。我所做的只是将JavaScript移到主体(JSFIDLE设置)并向页面添加一个“quantity”元素,因为它丢失了。为什么
函数ordersuminary()
函数calculate()和
中(init=document.getElementsByClassName(“final”)[I++]{
?谢谢!!效果很好。关于声明orderSummary()函数,将其放在calculate函数中有害吗,或者这更像是一个最佳实践原因?顺便问一下,如何让内联代码显示在注释中?我是一个noob..无法理解这只是最好的实践,不嵌套函数声明,使函数在父函数之外可用。在注释中编写代码用反勾号
`
->
`$(function(){…}`
包装代码。若要包含反勾号,请将其转义->
\`