【问题标题】:How to set value to input field with cakephp 2.x?如何使用 cakephp 2.x 为输入字段设置值?
【发布时间】:2015-10-19 17:39:18
【问题描述】:

我需要在 cakephp 2.x 中为输入文本字段设置一个值。

说明: 我有一个选择字段,其中显示文章列表,具体取决于用户选择的文章,我必须在输入文本字段中设置这篇文章的价格(只读)。

我正在使用 JavaScript。这是我的代码:

<script type="text/javascript">

    function alerta(a){
           var price = ?//something in a function within my controller.
           document.getElementById("AperturaArticuloPrecio").value = "price";
    }

</script> 

我的输入字段:

echo "<table><tr><td>";
echo $this->Form->input('articulo_id',array('onChange' => 'alerta(1)'));
echo "</td><td>";
echo $this->Form->input('cantidad',array('type' => 'text'));
echo "</td><td>";
echo $this->Form->input('precio',array('type' => 'text','readonly' => 'readonly'));
echo "</td><td>";
echo $this->Form->input('total',array('type' => 'text','readonly' => 'readonly'));
echo "</td></tr></table>";

我知道我的javascript代码中的'a'变量必须是文章的名称,我需要使用mi控制器获取这篇文章的价格,但我不知道如何使用javascript调用控制器.

如果您需要更多详细信息,请告诉我。谢谢。


更新:

我为下一个代码更改了我的 javascript 代码:

    <?php
        $this->Js->get('#AperturaArticuloArticuloId')->event('change', '
                //alert($( "#AperturaArticuloArticuloId" ).val());
                $( "#AperturaArticuloPrecio" ).val($( "#AperturaArticuloArticuloId option:selected" ).text());
                //Here something to call my function in my controller
            ');
        echo "<table><tr><td>";
        echo $this->Form->input('articulo_id');
        echo "</td><td>";
        echo $this->Form->input('cantidad',array('type' => 'text'));
        echo "</td><td>";
        echo $this->Form->input('precio',array('type' => 'text','readonly' => 'readonly'));
        echo "</td><td>";
        echo $this->Form->input('total',array('type' => 'text','readonly' => 'readonly'));
        echo "</td></tr></table>";
    ?>

所以我可以获取文章的索引及其内容,我只需要将这些值发送到我的控制器中的一个函数。我怎么能这样做? - 我需要使用 AJAX、Json 还是其他的东西?

在我的控制器中,我有这个功能,但我不知道如何访问它:

public function getPrice($id = null) {

    $options = array(
        'fields' => array('Articulo.precio'),
        'conditions' => array('Articulo.id' => $id));
    $price = $this->Articulo->find('list', $options);

    echo $price;
}   

我会很感激你的链接!呵呵

【问题讨论】:

  • 你需要设置一个值,为什么你在这里使用javascript? echo $this->Form->input('total',array('type' => 'text','readonly' => 'readonly' value = 'myvalue'));
  • 使用onchange函数并通过AJAX调用你的结果。

标签: javascript php jquery cakephp


【解决方案1】:

好吧,我在这里搜索:http://book.cakephp.org/2.0/en/core-libraries/helpers/js.htmlupdate form field with js but not showing in submitted data 混合在一起

我的解决方案是:

    <?php
        $data = $this->Js->get('#AperturaArticuloArticuloId')->serializeForm(array('inline' => true));

        $this->Js->get('#AperturaArticuloArticuloId')->event(
            'change',
            $this->Js->request(
                array(
                    'action' => 'getPrice', 
                    'controller' => 'articulos'
                ),
                array(
                    //'update' => '#AperturaArticuloPrecio',
                    'success' => '
                        $("#AperturaArticuloPrecio").val(data);
                        //More javascript code here
                    ',
                    'data' => $data,
                    'async' => true,    
                    'dataExpression'=>true,
                    'method' => 'POST'
                )
            )
        );

        echo $this->Js->writeBuffer();

        echo "<table><tr><td>";
        echo $this->Form->input('articulo_id');
        echo "</td><td>";
        echo $this->Form->input('cantidad',array('type' => 'text'));
        echo "</td><td>";
        echo $this->Form->input('precio',array('type' => 'text','readonly' => 'readonly'));
        echo "</td><td>";
        echo $this->Form->input('total',array('type' => 'text','readonly' => 'readonly'));
        echo "</td></tr></table>";
    ?>

在我的控制器中:

public function getPrice() {
    $this->autoRender = false;

    $options = array(
        'fields' => array('Articulo.precio'),
        'conditions' => array('Articulo.id' => $this->request->data['AperturaArticulo']['articulo_id']));

    $price = $this->Articulo->find('list', $options);

    $article_selected = implode(",", $price);

    echo "$".$article_selected;
}   

感谢大家帮助我!

【讨论】:

    猜你喜欢
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2019-05-07
    • 2013-04-20
    • 1970-01-01
    相关资源
    最近更新 更多