0

我有这个表:

客户[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.ctpCustomerServiceTypes 中,我有一个包含服务类型的下拉列表和一个包含价格的字段。我想要的是当用户从下拉列表中选择服务类型时,然后使用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'));
    }
4

1 回答 1

0

您可以创建一个 js 函数,使用选择框的 onchange 属性调用该函数,然后在获得结果后立即更改输入字段值。

作为参考,您可以查看当用户从选择框中选择选项时更改输入字段的值

于 2018-10-05T12:52:55.843 回答