【发布时间】: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