我正在使用这个 js 从我的数据库 MYSQL 中恢复一些数据,它工作得很好。
<script type = "text/javascript" >
$(document).ready(function() { // When the document is ready
$("select#product1").on("change", function() { // We attach the event onchange to the select element
var id = this.value; // retrive the product id
$.ajax({
url: "ajax.php", // path to myphp file which returns the price
method: "post", // POST request
data: "id=" + id, // I retrieve this data in my$_POST variable in ajax.php : $_POST[id]
success: function(response) { // The function to execute if the request is a -success-, response will be the price
$("input#total").val(response); // Fill the price
}
});
});
}); </script>
在我的输入#total 中我得到这个值(例如):
[{"价格":"5","时间":"10"}]
我怎样才能只得到价格值或时间值? 我搜索了一些 json 解析器,但无法将其放入我的输入中。
这是我的完整 HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="j_idt2">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("select#product1").on("change", function() {
var id = this.value;
$.ajax({
url: "ajax.php",
method: "post",
data: "id=" + id,
success: function(response) {
var datashow = jQuery.parseJSON(response);
$("input#total").val(response);
}
});
});
});
</script>
</head>
<body>
<select id="product1" name="product1">
<option value="" selected="selected">-</option>
<option value='17'>Product1</option>
</select>
<input id="total" type="text" name="total" class="form-control" />
</body>
</html>
这是我使用 $("input#total").val(response); 选择 product1 时进入输入字段的内容;
[{"价格":"5","时间":"5"}]
当我改变这一行时:
$("input#total").val(response);
与
$("input#total").val(datashow.price);
我没有得到任何东西。
我的 php 似乎可以正确呈现我的 json。看起来我的回复未被解析或无法以任何方式存储到 var 中。
最佳答案
使用 jQuery.parseJson 解析你的响应 例如,像下面这样更改您的代码
<script type = "text/javascript" >
$(document).ready(function() { // When the document is ready
$("select#product1").on("change", function() { // We attach the event onchange to the select element
var id = this.value; // retrive the product id
$.ajax({
url: "ajax.php", // path to myphp file which returns the price
method: "post", // POST request
data: "id=" + id, // I retrieve this data in my$_POST variable in ajax.php : $_POST[id]
success: function(response) { // The function to execute if the request is a -success-, response will be the price
var datashow = JSON.parse(response);
$("input#total").val(datashow[0].price);
}
});
});
}); </script>
希望这能解决你的问题
/////////////////////尝试使用静态响应///////////
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="j_idt2">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var response = [{"price":"5","time":"5"}];
$("input#total").val(response[0].price);
});
</script>
</head>
<body>
<select id="product1" name="product1">
<option value="" selected="selected">-</option>
<option value='17'>Product1</option>
</select>
<input id="total" type="text" name="total" class="form-control" />
</body>
</html>
我得到了正确的结果。
关于php - 获取解析为输入字段的 json 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826933/