Javascript 不检查组合网格更改的文本输入值

标签 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 可以正常工作,并且它会检查所选值何时发生变化。 如果我使用带有简单文本输入的组合网格,它无法正常工作,并且 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;

            })
        });

}              
        }                        
    })

但是这不起作用。不知道有没有什么问题。

最佳答案

您需要使用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/

关于Javascript 不检查组合网格更改的文本输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27464199/

相关文章:

php - 这个 INSERT 查询有什么问题?

php - python:在拦截器中读取grpc proto请求

php - 如何通过php连接sftp服务器

python - 使用 mysql-connector-python 从 mysql 中选择数据

javascript - 点击功能中的 sencha touch HTMLDivElement

javascript - 在 Javascript 中使用警报时出错(对象的属性 'alert' 不是函数)

php - 在 MYSQL 中有效地连接 3 个表

mysql - 按从存储过程 MYSQL 中调用的函数返回的值排序

javascript - 单击 block 时如何不计算子 block (示例 .child 跨度)?

javascript - ExcelJS Node : horizontalCentered & verticalCentered in page not working