JSON无法与OnChange事件一起使用 [英] JSON not working with OnChange event

查看:100
本文介绍了JSON无法与OnChange事件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初编码这部分代码是为了带回单个数据点.我意识到我需要通过onchange事件(下拉选择)返回的更多字段,并将其更改为使用JSON.它不返回任何数据.首次加载页面时,下拉列表是通过PHP动态构建的.我是新来的,因此不胜感激.

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.

下拉代码:

<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 .= "\r\n<option value= 'NA'>Select Category</option>";
                foreach ($stmt as $row) {
                  $dropdown .= "\r\n<option value='{$row['ID']}'>{$row['RPT_NAME']}</option>";
                }

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

Ajax/JSON代码:

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代码:

<?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);
?>

推荐答案

所以,我已经纠正了您的错误,除非有其他原因导致错误,否则您应该具有以下内容才能使它正常工作.

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
        }
    });
});

这篇关于JSON无法与OnChange事件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆