2015-10-19 42 views
0

我需要为cakephp 2.x中的输入文本字段设置一个值。如何用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; 
} 

我将是一个链接感谢! XD

+0

你需要设置一个值,为什么你在这里使用javascript? ('total',array('type'=>'text','readonly'=>'readonly'value ='myvalue')); –

+0

使用onchange函数并通过AJAX调用您的结果。 –

回答

0

嗯,我正在寻找在这里:http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html和我一起update 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; 
} 

谢谢大家的帮助我!

相关问题