将AJAX响应设置为javascript变量以供重用
编辑:找到了解决方案,我编辑了代码,所以如果有人在不久的将来遇到同样的问题,可以复制我的代码 我需要将AJAX响应设置为变量,以便在下一个js脚本中使用它。如何将AJAX响应设置为变量? 我不太擅长javascript,所以可能只是打字错误之类的。 这是我的密码将AJAX响应设置为javascript变量以供重用,javascript,jquery,ajax,Javascript,Jquery,Ajax,编辑:找到了解决方案,我编辑了代码,所以如果有人在不久的将来遇到同样的问题,可以复制我的代码 我需要将AJAX响应设置为变量,以便在下一个js脚本中使用它。如何将AJAX响应设置为变量? 我不太擅长javascript,所以可能只是打字错误之类的。 这是我的密码 <script type="text/javascript"> var delivery; // declare of variable to make it "global" $(document).ready(funct
<script type="text/javascript">
var delivery; // declare of variable to make it "global"
$(document).ready(function() {
$("#jumlah").bind("input change paste keyup", function() {
var qty = $(this).val();
$.ajax({
type: 'POST',
url: '../component/quantity.php',
data: {
jumlah: qty,
id:<?php echo $hasil['id']; ?>
},
success: function (response) {
// We get the element having id of display_info and put the response inside it
delivery = parseFloat(response); // remove the "var" here so you take the cur variable and don't set a new one in the scope of this function.
}
});
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$("select").change(function() {
var total = delivery;
$('select option:selected').each(function() {
total += parseFloat($(this).data('price'));
});
var updatePrice = document.getElementById('jumlah').value;
var grandTotal = total * updatePrice;
$(".total").val(grandTotal);
$(".total").html(grandTotal.toLocaleString());
});
$("#jumlah").bind("input change paste keyup", function() {
var total = delivery;
$('select option:selected').each(function() {
total += parseFloat($(this).data('price'));
});
var updatePrice = $(this).val();
var grandTotal = total * updatePrice;
$(".total").val(grandTotal);
$(".total").html(grandTotal.toLocaleString());
});
});
</script>
风险值交付;//声明变量的名称,使其成为“全局”
$(文档).ready(函数(){
$(“#jumlah”).bind(“输入更改粘贴键控”,函数(){
变量数量=$(this.val();
$.ajax({
键入:“POST”,
url:“../component/quantity.php”,
数据:{
朱姆拉:数量,
身份证件:
},
成功:功能(响应){
//我们获取id为display_info的元素,并将响应放入其中
delivery=parseFloat(response);//删除此处的“var”,这样就可以获取cur变量,而不必在此函数的作用域中设置新变量。
}
});
});
});
$(文档).ready(函数(){
$(“选择”).change(函数(){
var总计=交付;
$('select option:selected')。每个(函数(){
总计+=parseFloat($(this).data('price');
});
var updatePrice=document.getElementById('jumlah').value;
var grandTotal=总计*更新价格;
美元(“.total”).val(总计);
$(“.total”).html(grandTotal.tolocalString());
});
$(“#jumlah”).bind(“输入更改粘贴键控”,函数(){
var总计=交付;
$('select option:selected')。每个(函数(){
总计+=parseFloat($(this).data('price');
});
var updatePrice=$(this.val();
var grandTotal=总计*更新价格;
美元(“.total”).val(总计);
$(“.total”).html(grandTotal.tolocalString());
});
});
您要查找的是范围
。根据设置变量的方式和位置,范围会发生变化。
在您的情况下,您需要一个全局可访问的,因此您应该将其放在顶部。您只需要声明它,不需要分配任何值
<script type="text/javascript">
var delivery; // declare of variable to make it "global"
$(document).ready(function() {
$("#jumlah").bind("input change paste keyup", function() {
var qty = $(this).val();
$.ajax({
type: 'POST',
url: '../component/quantity.php',
data: {
jumlah: qty,
id:1
},
success: function (response) {
// We get the element having id of display_info and put the response inside it
delivery = parseFloat(response); // remove the "var" here so you take the cur variable and don't set a new one in the scope of this function.
}
});
});
});
</script>
风险值交付;//声明变量的名称,使其成为“全局”
$(文档).ready(函数(){
$(“#jumlah”).bind(“输入更改粘贴键控”,函数(){
变量数量=$(this.val();
$.ajax({
键入:“POST”,
url:“../component/quantity.php”,
数据:{
朱姆拉:数量,
身份证号码:1
},
成功:功能(响应){
//我们获取id为display_info的元素,并将响应放入其中
delivery=parseFloat(response);//删除此处的“var”,这样就可以获取cur变量,而不必在此函数的作用域中设置新变量。
}
});
});
});
第二部分
<script type="text/javascript">
$(document).ready(function() {
$("select").change(function() {
var total = delivery;
$('select option:selected').each(function() {
total += parseFloat($(this).data('price'));
});
var updatePrice = document.getElementById('jumlah').value;
var grandTotal = total * updatePrice;
$(".total").val(grandTotal);
$(".total").html(grandTotal.toLocaleString());
});
$("#jumlah").bind("input change paste keyup", function() {
var total = delivery;
$('select option:selected').each(function() {
total += parseFloat($(this).data('price'));
});
var updatePrice = $(this).val();
var grandTotal = total * updatePrice;
$(".total").val(grandTotal);
$(".total").html(grandTotal.toLocaleString());
});
});
</script>
$(文档).ready(函数(){
$(“选择”).change(函数(){
var总计=交付;
$('select option:selected')。每个(函数(){
总计+=parseFloat($(this).data('price');
});
var updatePrice=document.getElementById('jumlah').value;
var grandTotal=总计*更新价格;
美元(“.total”).val(总计);
$(“.total”).html(grandTotal.tolocalString());
});
$(“#jumlah”).bind(“输入更改粘贴键控”,函数(){
var总计=交付;
$('select option:selected')。每个(函数(){
总计+=parseFloat($(this).data('price');
});
var updatePrice=$(this.val();
var grandTotal=总计*更新价格;
美元(“.total”).val(总计);
$(“.total”).html(grandTotal.tolocalString());
});
});
编辑:修复了代码。变量必须在函数外部。由于
ajax
调用是异步的,因此无法确定传递
变量是否未定义。
在您的情况下,这也无法工作,因为变量是在回调
范围中定义的。要确保已定义交付响应,请创建如下函数:
function onDelivery(delivery) {
....(rest of second script)
}
将此功能传递给success
并按原样使用delivery
。尝试以下操作:-
<script type="text/javascript">
var delivery; // make your variable delivery as global so you can use it where you want.
$(document).ready(function() {
//you can use it here or anywhere in script you want to re-use it.
//put your code as per your functionality.
});
</script>
风险值交付;//使您的变量交付成为全局的,这样您就可以在您想要的地方使用它。
$(文档).ready(函数(){
//您可以在此处或脚本中的任何位置使用它,以便重复使用。
//根据您的功能放置代码。
});
窗口对象表示浏览器中打开的窗口
窗口
是一个对象,您可以将任何属性添加到窗口
success: function (response) {
window.deliveryResponse = response;
}
因此,您可以在任何其他js文件中使用此响应
other.js
(window.deliveryResponse) && console.log(window.deliveryResponse)
几乎可以工作,但由于某些原因,它没有返回正确的值。当我在#jumlah输入上设置“1”时,我期望值为“15.000”。不知怎的,它返回“1500.000”。我不知道为什么会这样。你能帮我吗?我编辑了我的答案,因为我把变量放错地方了。但你所描述的听起来像是一个格式化错误。填充后立即记录时,该值是否正确?已通过在div上回显测试了“delivery”值。该值正确。这个问题似乎出现在脚本的第二部分。但是我之前已经测试了第二个脚本,它运行得很好。我在第二部分没有做任何更改。但我想这可能是因为总量/交付量是一个字符串。只需尝试delivery=parseFloat(响应)。我编辑我的答案。它正在工作!谢谢。几周后我可以继续了。嘿,你能用更多的例子解释一下吗?我真的不懂javascript。但我认为你的解决方案可以解决我的问题