从 onChange 事件调用控制器操作
我需要一些有关 Yii 的帮助。
如何从文本框 onChange 事件调用控制器操作?
更新: 我有一个用 CActiveForm 构建的表单。该表单有许多文本框。当我更改任何值时,必须更新其他值,使用新输入的值计算公式。所以我想做的是:
<tr>
<td>Average KW Demand:</td>
<td><?php echo $form->textField($lfac_model, 'kw_demand', array('size'=>5));?></td>
</tr>
<tr>
<td>Estimated Load Factor:</td>
<td><?php echo $form->textField($lfac_model, 'loadf', array('size'=> 5));?>/td>
</tr>
当用户更改“估计负载系数”时,“平均千瓦需求”值应使用公式的结果进行更新。我想到的是将公式存储在表中,然后转到控制器操作,检索特定公式,计算新值并将其发送回视图。我不确定这是否是实现这一点的最佳想法。
I need some help with Yii.
How can I call a Controller action from a textbox onChange event?
Update:
I have a form built with CActiveForm. This form has many text-boxes. When I change any value other must be updated computing a formula using the new entered value. So what I am trying to do is the following:
<tr>
<td>Average KW Demand:</td>
<td><?php echo $form->textField($lfac_model, 'kw_demand', array('size'=>5));?></td>
</tr>
<tr>
<td>Estimated Load Factor:</td>
<td><?php echo $form->textField($lfac_model, 'loadf', array('size'=> 5));?>/td>
</tr>
When the user changes "Estimated Load Factor", "Average KW Demand" value should be updated with the result of a formula. What I've figured out is to store the formulas in a table, then go to a Controller action, retrieve the specific formula, calculate the new value and send it back to the view. I'm not sure if this is the best idea to implement this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够使用 CJuiAutoComplete Yii 小部件来执行您想要的操作。 此处有一个基本演示,并且还有其他示例Yii 文档。在您的情况下,您需要确保的主要事情是您还指定了“appendTo”选项,请参阅 jQuery 自动完成文档,该小部件使用的。
因此基本上,您将为“估计负载系数”字段设置小部件,并通过appendTo 属性指向“平均功率需求”字段。
如果您不想要自动完成功能而只想替换该值,您可以在“loadf”值上使用 htmlOptions 属性“ajax”,如下所示:
请参阅: CHtml#ajax-detail 和 jQuery.ajax。在这种情况下,您很可能会返回纯文本,因此您的控制器函数将如下所示:
You should be able to use the CJuiAutoComplete Yii widget to do what you want. There is a basic demo here and there are other examples in the Yii docs. The main thing you would need to make sure in your case is that you also specify the "appendTo" option, see the jQuery autocomplete docs, which that widget uses.
So basically, you would set up the widget for your "Estimated Load Factor" field, and point to the "Average KW Demand" field via the appendTo attribute.
And if you don't want the autocomplete stuff and simply want to replace the value, you can use the htmlOptions attribute "ajax" on your "loadf" value like so:
see: CHtml#ajax-detail and jQuery.ajax. In this case you would most likely return plain text so your controller function would look like:
文本框的
onChange
事件是一个 JavaScript 事件。它存在于应用程序的不同方面。要在控制器上调用操作,您必须使用一些AJAX(或异步调用)。这里的想法本质上是将一些数据发布到处理该操作的路由。
例如:
/product/details
=>ProductController::actionDetails()
onChange
事件上设置 javascript,以调用将字段值(当前输入的文本)发布到控制器上的操作的方法。/product/ajaxautocomplete
=>ProductController:actionAjaxAutocomplete()
您可以使用 jQuery 轻松完成此任务:
A Textbox's
onChange
event is a javascript event. It lives in a different aspect of your application.To call an action on your controller you're going to have to use some AJAX (Or an asynchronous call). The idea here is to essentially post some data to the route that handles that action.
For instance:
/product/details
=>ProductController::actionDetails()
onChange
event of your search field to call a method that posts the field value (currently entered text) to an action on your controller./product/ajaxautocomplete
=>ProductController:actionAjaxAutocomplete()
You can use jQuery to accomplish this easily: