doukan4039 2015-03-23 16:08
浏览 77
已采纳

JSON无法使用OnChange事件

I originally coded this piece to bring back a single data point. I realized I needed more fields returned with an onchange event (drop down select) and changed it to use JSON. It is not returning any data. The drop down is built dynamically through PHP when the page is first loaded. I'm new to this, so any help would be much appreciated.

Dropdown Code:

<p id="dropdown"   style="DISPLAY: none" >
        <?php
            $query = "call test.spsMSTR_AllCatListBuild";
            $stmt = $conn->query( $query );

            $dropdown = "<select id='catlist' name='catlist' onchange='getval(this);'>";
                $dropdown .= "
<option value= 'NA'>Select Category</option>";
                foreach ($stmt as $row) {
                  $dropdown .= "
<option value='{$row['ID']}'>{$row['RPT_NAME']}</option>";
                }

                $dropdown .= "
</select>";
                echo $dropdown;
                $conn = null;
        ?>
    </p>

Ajax/JSON code:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<script id="source" language="javascript" type="text/javascript">

       $(document).ready(function(){
            $("#catlist").change(function(){
            var vid = document.getElementById("catlist").value;

            $.getjson("ajax.php",
            {catid:vid},
            function(result){
                alert(result); }
                .error(function(xhr) {
                    alert(xhr)
                })
                ; )
        })})

  </script>

PHP Code:

<?php include('./DBConfig.php'); ?>
<?php

    $vid = $_GET["catid"];
    $query =  "SELECT RPT_NAME, ACTIVE FROM test.MSTR_REPORT_MASTER WHERE ID = $vid" ;
    $stmt = $conn->query( $query );
    //$result = $stmt->fetchColumn();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    echo json_encode($result);
?>
  • 写回答

2条回答 默认 最新

  • dongsutao8921 2015-03-23 16:24
    关注

    So, I have corrected your errors and you should have the below for it to work with no errors, unless there is something else that is causing errors.

    <select id='catlist' name='catlist'>
        <option value='NA'>Select Category</option>
        <option value='id1'>val1</option>
        <option value='id2'>val2</option>
        <option value='id3'>val3</option>
    </select>
    
    $(function(){
        $("#catlist").change(function() {
            var sVal = $(this).val();
            if (sVal !== "NA") { // I am assuming you don't want to make an ajax call when the value is 'NA'!
                $.getJSON("ajax.php", {catid: $(this).val()}, function(result) {
                    alert(result); 
                });
                //by the way $(this).val() = id1|id2|id3
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?