php - 获取解析为输入字段的 json 值

标签 php jquery mysql json ajax

我正在使用这个 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/

相关文章:

mysql - SQL 类语句 "%[^0-9]%"不工作

php - 使用 JavaScript 截取 url 的 GET 部分,包括?

仅适用于某些 child 的 PHP 树依赖列表

jquery - 如何在 webpack 中使用语义 ui 进行 react ?

javascript - 如何在 jquery html 字符串中包含内联 onclick

php/mysql动态菜单

php - MySQL数据库和导入日期问题

php - Codeigniter 如何设置 API key 和资源 URL 以便在应用程序中轻松访问

javascript - 滚动函数触发多次而不是一次

mysql - 需要帮助组合两个 MySQL 查询