javascript - CakePHP 3.6.11:根据下拉值更新字段
问题描述
我有这个表:
客户[id, name, surname, phone, text, balance, created]
service_types[id, title, price, length, is_subscription, created]
customer_service_types[id, customer_id, service_type_id, price, created]
在add.ctp
CustomerServiceTypes 中,我有一个包含服务类型的下拉列表和一个包含价格的字段。我想要的是当用户从下拉列表中选择服务类型时,然后使用price
所选服务类型更新价格字段。
这是现有的代码:
add.ctp
<fieldset>
<legend><?= __('Add Customer Service Type') ?></legend>
<?php
echo $this->Form->control('customer_id', ['options' => $customers]);
echo $this->Form->control('service_type_id', ['options' => $serviceTypes]);
echo $this->Form->control('price');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
CustomerServiceTypesController.php
(添加功能)
public function add()
{
$customerServiceType = $this->CustomerServiceTypes->newEntity();
if ($this->request->is('post')) {
$customerServiceType = $this->CustomerServiceTypes->patchEntity($customerServiceType, $this->request->getData());
if ($this->CustomerServiceTypes->save($customerServiceType)) {
$this->Flash->success(__('The customer service type has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The customer service type could not be saved. Please, try again.'));
}
$customers = $this->CustomerServiceTypes->Customers->find('list', ['limit' => 200]);
$serviceTypes = $this->CustomerServiceTypes->ServiceTypes->find('list', ['limit' => 200]);
$this->set(compact('customerServiceType', 'customers', 'serviceTypes'));
}
解决方案
您可以创建一个 js 函数,使用选择框的 onchange 属性调用该函数,然后在获得结果后立即更改输入字段值。
作为参考,您可以查看当用户从选择框中选择选项时更改输入字段的值
推荐阅读
- ios - UITabBar 项目标题位置问题
- amazon-web-services - cloudfront - lambda 函数 - 使用 KMS 的 s3 加密
- azure - Azure WebJob 未从 QueueTrigger 触发
- javascript - Mongoose:计算数组元素
- c++ - 将 RGBA 二值图像转换为 YCbCr 但结果不如预期
- python - 如何从python中的yyyyww列中提取一周的最后一天
- flutter - Flutter Web URL 导航,如 http://localhost:60117/ItemDetails?pId=1 不起作用
- linux - 使用 linux 选项发布的 winforms 应用程序是否在 linux 操作系统上运行?
- javascript - 是否有可能在伊斯坦布尔 JS 覆盖率报告中查看哪些测试实际上负责覆盖这条线?
- python - 使用 Python 将此 Json 文件添加到 mySQL 数据库中