0

我有这 3 张桌子:

顾客:

客户表

服务:

服务台

客户服务:

客户服务稳定

有了这个关系CustomerservicesTable.php

$this->belongsTo('Customers')
            ->setForeignKey('customerid');

$this->belongsTo('Services')
            ->setForeignKey('serviceid');

Template\Customerservices\add.ctp我有一个带有下拉列表和数字字段的表单:

<div class="customerservices form large-9 medium-8 columns content">
    <?= $this->Form->create($customerservice) ?>
    <fieldset>
        <legend><?= __('Add transaction') ?></legend>
        <?php
            echo $this->Form->input('Transaction type',array('options' => $servicesList));
            echo $this->Form->control('price');
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

Controller\CustomerservicesController.php

public function add($customerid = null)
    {

        $customerservice = $this->Customerservices->newEntity();
        if ($this->request->is('post')) {
            $customerservice->customerid = $customerid;
            $customerservice->serviceid = //get selection from dropdown
            if ($this->Customerservices->save($customerservice)) {
                $this->Flash->success(__('The customerservice has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The customerservice could not be saved. Please, try again.'));
        }
        $this->set(compact('customerservice'));

        $servicesList = TableRegistry::getTableLocator()->get('Services')->find('list');
        $this->set(compact('servicesList'));
    }

如何替换评论以保存serviceid在下拉控件中选择的内容?

(第二个问题是否可以price根据下拉选择隐藏该字段?)

4

1 回答 1

0

就像我几分钟前告诉你的那样。

像这样改变input

echo $this->Form->input('transaction_type',array('type'=>'select','options' => $servicesList));

在你的Controller

public function add($customerid = null)
{
    …
    $customerservice->serviceid = $this->request->getData('transaction_type');
    …
}

根据下拉菜单中的选择隐藏price字段似乎是客户端的工作,可以使用 JavaScript 完成。例如在 jQuery 中:

$('#transaction_type').on('change', function() {
    // hide element with ID #price if value of select with ID #transaction_type is `holymoly`
    // and show element if value is anything else
    $('#price').toggle(this.value === 'holymoly');
}); 
于 2018-09-27T14:43:24.017 回答