[英]Javascript doesn't check text input value changing with combogrid
在其他討論中,我嘗試了不同的方法來檢查文本輸入的值是否發生變化。 但是我還沒有找到適合我的解決方案。 你能幫助我嗎? 我在此主頁上有一個表格:
<?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=''>  Gearbox 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=''>  No</label><input type="radio" id="sunroof1" name="sunroof" value="no" >
<label for=''>  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;
})
});
}
}
})
但這是行不通的。 不知道有什么問題
您需要使用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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.