duandi4238 2014-12-13 22:16
浏览 83
已采纳

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

I've tried different way, proposed in other discussions, to check if the value of an text input change. But I've not found a solution that works in my case. Can you help me? I've a form this main page:

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

When the user select a value from the fist text input the others text input, radio button and dropdown menu have to be setted according to the value loaded from mysql database. If i use a simple select input to select a car added to the database the javascript works fine and it check when the selected value change. If i use combogrid with a simple text input it doesn't works fine and javascript doesn't check any changes and data are non loaded from database with ajax. How can i solve?

UPDATE 1

I've tried in this way:

    $(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;

            })
        });

}              
        }                        
    })

But it doesn't work. I don't know if there is something wrong.

  • 写回答

1条回答 默认 最新

  • duanchun1909 2014-12-14 15:13
    关注

    You need to use the onSelect:function(record){...} event to get the selected value.

    <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>
    

    here is a jsFiddle example - http://jsfiddle.net/kuv5ao8f/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?