簡體   English   中英

json數據未在jquery中顯示

[英]json data not shown in jquery

Html代碼是:

<select  name="ser" id="ser" class="form-control" onchange="getPrice(this.value);">
<option value="">--Select--</option>
<option value="Value11">Value1</option>
<option value="Value2">Value2</option>
</select>
<input type="text" name="sale" id="saleprice"  class="form-control" />
<input type="text" name="sale" id="saletax"  class="form-control" />
<input type="text" name="sale" id="avalqty"  class="form-control" />

在我的Js頁面上:

function getPrice(val)
{
   $.ajax({
     type: 'post',
     url: 'get_sales_price.php',
     data: {
       get_option:val
     },
     success: function (response) {
        var valuesar = response.split("|");
        $('#saleprice').val(valuesar[0]);
        $('#saletax').val(valuesar[1]);
        $('#avalqty').val(valuesar[2]);
     }
   });
}

這是我的PHP頁面數據:

$data = array();    
$values=$variable1['value1'].'|'.$variable2['value2'].'|'.$variable3;
array_push($data, $values);
echo json_encode($data);

#saleprice上的值是: [“61.25#avalqty上的值是: 155”] ,而#saletax上的值是:1。 #saletax值是正確的..如何獲得#saleprice[“61.2561.25#avalqty155“155

我認為你可以做的是從服務器返回一個key-value對象,並在成功處理程序中使用它。 在您的情況下,您將返回一個具有單個字符串值的數組

$data = array();    
$data['price'] = $variable1['value1'];
$data['tax'] = $variable2['value2'];
$data['qty'] = $variable3;
echo json_encode($data);

然后

function getPrice(val) {
  $.ajax({
    type: 'post',
    url: 'get_sales_price.php',
    data: {
      get_option: val
    },
    dataType: 'json',
    success: function(response) {
        console.log(response)
      $('#saleprice').val(response.price);
      $('#saletax').val(response.tax);
      $('#avalqty').val(response.qty);
    }
  });
}

在你的成功函數中嘗試JSON.parse(response)。 您是在數組中發送字符串,因此您需要先解析它,使用數組索引訪問該字符串,然后拆分此字符串。 否則,您可以直接在PHP代碼中回顯字符串,而不是在數組中推送它,然后使用json_encode。

所以你的PHP代碼應該是

$values=$variable1.'|'.$variable2.'|'.$variable3;
echo $data;

這樣,您將在ajax響應中收到一個字符串,您可以直接拆分。

更改

var valuesar = response.split("|");

 var valuesar = response[0].split("|");

嘿,您可以按照以下方式使用您的代碼

<?php
$data = array();    
$values='6.15'.'|'.'52.22'.'|'.'55.25';
array_push($data, $values);

$rr=json_encode($data);


$ltrim = ltrim($rr, '["');
$rtrim = rtrim($ltrim, '"]');


// echo $rtrim;



?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">

jQuery(document).ready(function() {


var response='<?php echo  $rtrim ?>';
console.log(response);

var valuesar  = response.split("|");


$('#saleprice').val(valuesar[0]);
$('#saletax').val(valuesar[1]);
$('#avalqty').val(valuesar[2]);

 console.log(valuesar[0]);

 });

 </script>
 <!DOCTYPE html>
 <html>
 <body>

<form>
<input type="text" name="saleprice" id="saleprice" >
<input type="text" name="saletax" id="saletax" >
<input type="text" name="avalqty" id="avalqty" >
</form> 

</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM