Javascript不检查使用combogrid更改的文本输入值

时间:2014-12-13 22:16:35

标签: javascript php mysql ajax keyup

我尝试过在其他讨论中提出的不同方式来检查文本输入的值是否发生变化。但我找不到适用于我的解决方案。你能帮助我吗? 我在这个主页上有一个表格:

<?php

$pdo = new PDO("mysql:host=localhost;dbname=db_nvh;charset=utf8", "root", "root");
?>
<html>
<head>
        <link type="text/css" rel="stylesheet" href="style.css"/>

            <link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
    <script type="text/javascript" src="easyui/jquery.min.js"></script>
    <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $('#id_car').combogrid({
                panelWidth:500,
                url: 'form5_getdata.php',
                idField:'id_car',
                textField:'id_car',
                mode:'remote',
                fitColumns:true,
                columns:[[
                    {field:'id_car',title:'Car ID',width:60},
                    {field:'brand',title:'Brand',align:'right',width:80},
                    {field:'model',title:'Model',align:'right',width:80},
                    {field:'chassis',title:'Chassis',align:'right',width:60},
                    {field:'db_proto',title:'Db proto',width:150}
                ]]
            });
        });
    </script>
         <script type="text/javascript">


        $(function() { // This code will be executed when DOM is ready
            $('#id_car').change(function() { // When the value for the id_car element change, this will be triggered
                var $self = $(this); // We create an jQuery object with the select inside
                $.post("getCarData.php", { id_car : $self.val()}, function(json) {
                    if (json && json.status) {
                        $('#brand').val(json.brand);
                        $('#model').val(json.model);
                        $('#gear_box_type').val(json.gear_box_type);
                      if(json.sunroof === 'yes'){
                    // var var_name = $("input[name='sunroof2']:checked").val();
                     $('#sunroof2').attr('checked', 'checked'); 
                    } else { // var var_name = $("input[name='sunroof1']:checked").val(); 
                     $('#sunroof1').attr('checked', 'checked'); } ;

                    }
                    var element = document.getElementById('gear_box_type');
                    element.value = json.gear_box_type;

                })
            });



        })
    </script>
        <title>RPT</title>
    </head>

    <body>
        <div id="content">
            <h1 align="center">Test page</h1>

            <form action="inserttraining.php" method="post">
                <div>
                    <p>
                 <input type="text" name="id_car" id="id_car" style="width:150px"></input>
                    <p>
                        Brand:
                        <input type="text" name="brand" id="brand">
                    </p>
                    <p>
                        Model:
                        <input type="text" name="model" id="model">
                    </p>

                    <?php
                    include("dbconnect.php");
$gear_box_typequery = "SELECT `gear_box_type` FROM `gear_box_type`";
$result = mysqli_query($conn,$gear_box_typequery);
echo "<label for=''>&thinsp;&thinsp;Gearbox&thinsp;Type:</label><br><select id='gear_box_type' name='gear_box_type'>";
while ($row = mysqli_fetch_array($result)) {
    echo "<option "; echo "value='" . $row['gear_box_type'] ."'>" . $row['gear_box_type'] ."</option>";
}
echo "</select>";
?>
                    <label for=''>&thinsp;&thinsp;No</label><input type="radio" id="sunroof1" name="sunroof" value="no" >
                    <label for=''>&thinsp;&thinsp;Yes</label><input type="radio" id="sunroof2" name="sunroof" value="yes">  

                    <input type="submit">
            </form>
        </div>

    </body>
</html>

当用户从第一个文本输入中选择一个值时,其他文本输入,单选按钮和下拉菜单必须根据从mysql数据库加载的值进行设置。 如果我使用一个简单的选择输入来选择添加到数据库的汽车,javascript工作正常,它会检查所选值的变化。 如果我使用带有简单文本输入的combogrid它不能正常工作,并且javascript不检查任何更改,并且数据未使用ajax从数据库加载。 我怎么解决?

更新1

我试过这样的方式:

    $(function() { 
        onSelect:function(record){ 
            $('#id_car').combogrid({ 
            var $self = $(this); // We create an jQuery object with the select inside
            $.post("getCarData.php", { id_car : $self.val()}, function(json) {
                if (json && json.status) {
                    $('#brand').val(json.brand);
                    $('#model').val(json.model);
                    $('#gear_box_type').val(json.gear_box_type);
                  if(json.sunroof === 'yes'){
                // var var_name = $("input[name='sunroof2']:checked").val();
                 $('#sunroof2').attr('checked', 'checked'); 
                } else { // var var_name = $("input[name='sunroof1']:checked").val(); 
                 $('#sunroof1').attr('checked', 'checked'); } ;

                }
                var element = document.getElementById('gear_box_type');
                element.value = json.gear_box_type;

            })
        });

}              
        }                        
    })

但它不起作用。我不知道是否有什么问题。

1 个答案:

答案 0 :(得分:0)

您需要使用onSelect:function(record){...}事件来获取所选值。

<script type="text/javascript">
    $(function(){
        $('#id_car').combogrid({
            panelWidth:500,
            url: 'form5_getdata.php',
            idField:'id_car',
            textField:'id_car',
            mode:'remote',
            fitColumns:true,
            columns:[[
                {field:'id_car',title:'Car ID',width:60},
                {field:'brand',title:'Brand',align:'right',width:80},
                {field:'model',title:'Model',align:'right',width:80},
                {field:'chassis',title:'Chassis',align:'right',width:60},
                {field:'db_proto',title:'Db proto',width:150}
            ]],

            // use onSelect: function(index,row) {...} to get selected row value, ie. row.id_car
            onSelect: function(index,row) {
                var $selected = row.id_car; // Get the selected row id_car
                $.post("getCarData.php", { id_car : $selected}, function(json) {
                    if (json && json.status) {
                        $('#brand').val(json.brand);
                        $('#model').val(json.model);
                        $('#gear_box_type').val(json.gear_box_type);
                        if(json.sunroof === 'yes'){
                            // var var_name = $("input[name='sunroof2']:checked").val();
                            $('#sunroof2').attr('checked', 'checked'); 
                        } else { // var var_name = $("input[name='sunroof1']:checked").val(); 
                            $('#sunroof1').attr('checked', 'checked'); } ;
                        }
                        var element = document.getElementById('gear_box_type');
                        element.value = json.gear_box_type;
                    }
                });
            }

        });
    });
</script>

这是一个jsFiddle示例 - http://jsfiddle.net/kuv5ao8f/