使用“自动选择”填充表单字段
Using Autoselect to populate form fields
我正在尝试创建一个php表单,该表单使用自动选择来选择项目,并从该选择中填充价格输入字段,然后填充小计和合计字段。
我有一张名为"配件"的表,它有以下字段:
accessories_id | accessories_name | accessories.price
我可以让第一个自动选择工作,但我不确定从那里去哪里。我对php没问题,但对Javascript我完全是个新手,不知道从哪里开始。
html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Autocomplete</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function(){
$("#accessories_name").autocomplete({
source:'getautocomplete.php',
minLength:1
});
});
$("#accessories_name").autocomplete({
source:'getautocomplete.php',
minLength:1,
change: function( event, ui ) {}
});
$( "#accessories_name" ).on( "autocompletechange", function( event, ui ) {
$("accessories_price").val(ui.item.value); //sets price with the value from selected accessory
// [...] other fields logic
} );
$( "#quantity" ).change(function() {
var subtotal = $("#accessories_price").val() * $("#quantity").val(); //don't forget to verify if are numbers
$("#subtotal").val(subtotal);
})
</script>
</head>
<body>
<form method="post" action="">
Name : <input type="text" size="40" id="accessories_name" name="accessories_name" />
Price: <input type="text" size="30" id="accessories_price" name="accessories_price" />
Quantity: <input type="text" size="30" id="quantity" name="quantity" />
Subtotal: <input type="text" size="30" id="subtotal" name="subtotal" />
<br />
Name : <input type="text" size="40" id="accessories_name" name="accessories_name" />
Price: <input type="text" size="30" id="accessories_price" name="accessories_price" />
Quantity: <input type="text" size="30" id="quantity" name="quantity" />
Subtotal: <input type="text" size="30" id="subtotal" name="subtotal" />
<br />
Total: <input type="text" size="40" id="accessories_total" name="accessories_total" />
</form>
</body>
</html>
php文件
<?php
mysql_connect("localhost", "db_user", "password");
mysql_select_db("db");
$term=$_GET["term"];
$query=mysql_query("SELECT * FROM accessories WHERE accessories_description LIKE '%".$term."%' ORDER by accessories_description");
$json=array();
while($accessories=mysql_fetch_array($query)){
$json[]=array(
'value'=>$accessories["accessories_name"],
'label'=>$accessories["accessories_name"]." - ".$accessories["accessories_id"]
);
}
echo json_encode($json);
?>
首先,您需要通过将change
回调添加到autocomplete
:中来初始化它
$("#accessories_name").autocomplete({
source:'getautocomplete.php',
minLength:1,
change: function( event, ui ) {} //add this to your current autocomplete
});
在您需要为change
回调创建一个侦听器之后,一旦在自动完成字段中选择了一个选项,就会调用它。在这个监听器中,你将有你的代码来更新价格和其他字段:
$( "#accessories_name" ).on( "autocompletechange", function( event, ui ) {
$("accessories_price").val(ui.item.value); //sets price with the value from selected accessory
// [...] other fields logic
} );
对于autocomplete
以外的字段,您可以使用jquery .change()
方法,如"#数量"情况,例如:
$( "#quantity" ).change(function() {
var subtotal = $("#accessories_price").val() * $("#quantity").val(); //don't forget to verify if are numbers
$("#subtotal").val(subtotal);
})
将此代码与$(document).ready({})
事件中逻辑所需的更改一起添加,并小心来自第二个和其他访问的ID
相关文章:
- CasperJS填充表单-输入名称有方括号
- 如何使用多维JSON数据重新填充表单
- 试图用cookie中的信息填充表单
- 如何在刷新时重新填充表单中的复选框并将表单值发布到页面
- 根据url填充表单选择下拉列表
- 从后台用angularjs填充表单
- 根据Rails4中的一个字段自动填充表单
- 使用jQuery用data-*数据填充表单文本输入
- 获取动态行数据值以使用 JavaScript 预填充表单
- 如何从 ng 重复项填充表单
- 如何使用 .ajax 拉取 MySQL 数据并用它填充表单
- 使用 php 变量值填充表单输入字段
- 轨道:填充表单中选择标签的输入
- 如何使用javascript用按钮填充表单列表
- 如何从我的 MySQL 数据库中提取数据以在页面加载时预填充表单
- 选择列表值并自动填充表单域
- 使用来自另一个表单的数据填充表单
- 如何使用 jquery 在同一页面上从数据库记录填充表单
- 基于表单输入值,如何 2 生成选择 4 下拉菜单 w 数据库中的行/记录数据,该数据库将在选择时预填充表单
- 如何使用我要编辑的 html 表中行中的数据填充表单中的输入字段