gpt4 book ai didi

javascript - 正确的功能顺序

转载 作者:行者123 更新时间:2023-11-27 23:19:11 25 4
gpt4 key购买 nike

我尝试在 onChange 事件上调用多个函数(请参阅index.php)。这些功能正在运行,但速度非常慢。函数的顺序应为:getPricegetTaxgetValuesgetValues2

当维持该序列时,脚本将正常工作。

getPrice      = 10.00
getTax = 5.00
__________+
getValues = 15.00 (total price)

函数的顺序现在为 getValuesgetValues2getPricegetTax。但这不是正确的顺序。现在我得到:

getPrice      = 10.00
getTax = 5.00
__________+
getValues = 0.00 (total price)

我需要第二次执行更改 getPricegetTax 以使 getValues 计算正确。

我的问题是。有没有办法来解决这个问题?如何以有效且正确的方式运行此脚本?

我使用以下代码:

index.php

<html>
<label>Quantity</label><br />
<input type="text" class="form-control" name="quantity1" id="quantity1" onChange='getPrice(this.value);getTax(this.value);getValues();getValues2()' placeholder="Quantity">
</html>

功能:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getPrice() {

// getting the selected id in combo
var selectedItem = jQuery('#product1 option:selected').val();

// Do an Ajax request to retrieve the product price
jQuery.ajax({
url: 'get.php',
method: 'POST',
data: {'product1': selectedItem, 'quantity1' : jQuery('#quantity1').val()},
success: function(response){
// and put the price in text field
jQuery('#price1').val(response);
},
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>

<script>
function getTax() {

// getting the selected id in combo
var selectedItem = jQuery('#product1 option:selected').val();

// Do an Ajax request to retrieve the product price
jQuery.ajax({
url: 'get11.php',
method: 'POST',
data: {'product1': selectedItem, },
success: function(response){
// and put the price in text field
jQuery('#tax1').val(response);
},
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>

<script>
function getValues(){
var numVal1 = Number(document.getElementById("price1").value);
var numVal2 = Number(document.getElementById("price2").value);
var numVal3 = Number(document.getElementById("price3").value);
var numVal5 = Number(document.getElementById("price4").value);

var totalValue = numVal1 + numVal2+numVal3+numVal5;
document.getElementById("total").value = totalValue;
}
</script>

<script>
function getValues2(){
var numVal1 = Number(document.getElementById("tax1").value);
var numVal2 = Number(document.getElementById("price1").value);

var totalValue = numVal1 + numVal2;
document.getElementById("lastprice").value = totalValue;
}
</script>

最佳答案

首先,这是一个软件架构问题。为什么您对每个操作都使用特定的请求?

有一些更好的解决方案:1. 制作一个主 Controller ,根据请求的参数获取和计算数据2. 在显示 View 时获取数据,然后使用 React 在前端计算它们,例如 ( https://facebook.github.io/react/ )3. 正如 Jaromanda 所说,您可以链接请求 - 每个请求都在前一个请求的完成功能中 ( https://laracasts.com/series/design-patterns-in-php/episodes/1 )

<script>
function getPrice() {

// getting the selected id in combo
var selectedItem = jQuery('#product1 option:selected').val();

// Do an Ajax request to retrieve the product price
jQuery.ajax({
url: 'get.php',
method: 'POST',
data: {'product1': selectedItem, 'quantity1' : jQuery('#quantity1').val()},
success: function(response)
{
// and put the price in text field
jQuery('#price1').val(response);

getTax();
},
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>

<script>
function getTax() {

// getting the selected id in combo
var selectedItem = jQuery('#product1 option:selected').val();

// Do an Ajax request to retrieve the product price
jQuery.ajax({
url: 'get11.php',
method: 'POST',
data: {'product1': selectedItem, },
success: function(response){
// and put the price in text field
jQuery('#tax1').val(response);

var numVal1 = Number(document.getElementById("price1").value);
var numVal2 = Number(document.getElementById("price2").value);
var numVal3 = Number(document.getElementById("price3").value);
var numVal5 = Number(document.getElementById("price4").value);

var totalValue = numVal1 + numVal2+numVal3+numVal5;
document.getElementById("total").value = totalValue;

var numVal1 = Number(document.getElementById("tax1").value);
var numVal2 = Number(document.getElementById("price1").value);

var totalValue = numVal1 + numVal2;
document.getElementById("lastprice").value = totalValue;
},
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>

关于javascript - 正确的功能顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35537045/

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