Javascript 如何使用cakephp 2.x设置输入字段的值?

Javascript 如何使用cakephp 2.x设置输入字段的值?,javascript,php,jquery,cakephp,Javascript,Php,Jquery,Cakephp,我需要在cakephp 2.x中为输入文本字段设置一个值 说明: 我有一个选择字段,在其中显示文章列表,根据用户选择的文章,我必须在输入文本字段只读中设置这篇文章的价格 我正在使用javascript。这是我的密码: <script type="text/javascript"> function alerta(a){ var price = ?//something in a function within my controller.

我需要在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> 

我会很感激的链接!XD

我在这里搜索:我和

我的解决办法是:

    <?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>";
    ?>

谢谢大家的帮助

您需要设置一个值,为什么在这里使用javascript?echo$this->Form->input'total',array'type'=>'text','readonly'=>'readonly'value='myvalue';使用onchange函数并通过AJAX调用结果。
    <?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>";
    ?>
public function getPrice($id = null) {

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

    echo $price;
}   
    <?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;
}