[英]Pass Element value to $ajax->link in cakephp
I need to pass the value of an element to an $ajax->link without using a form/submit structure. 我需要将元素的值传递给$ ajax-> link而不使用表单/提交结构。 (because I have a dynamically set number of clickable links through which I am triggering the action)
(因为我有动态设置的可点击链接数,通过它们触发操作)
I used to do this in Ruby using the Prototype javascript function $F like this: 我曾经在Ruby中使用Prototype javascript函数$ F做到这一点,如下所示:
<%= link_to_remote "#{item.to_s}",
:url => { :action => :add_mpc },
:with => "'category=' + $F('mpc_category')" -%>
But this does not seem to work in Cakephp: 但这在Cakephp中似乎不起作用:
<?php echo $ajax->link(substr($vehicle['vehicles']['year'], -2),
array('action' => 'add_mpc', 'category' => '$F("mpc_category")'),
array('update' => 'results', 'position' => 'top')); ?>
PHP sees $F as a variable instead of a call to javascript. PHP将$ F视为变量,而不是对javascript的调用。 I'm not too familiar with Javascript, but is there another way to pass the value of the 'mpc_category' input element to the controller through this link?
我不太熟悉Javascript,但是还有另一种方法可以通过此链接将“ mpc_category”输入元素的值传递给控制器吗? I have been looking for a couple days and can't find anyone dealing with this specific issue.
我已经找了几天,找不到任何处理此特定问题的人。 Thanks for any assistance.
感谢您的协助。
Edit: fixed syntax in php statement. 编辑:在php语句中的固定语法。
haven't really used cake, but I have used rails. 还没有真正使用蛋糕,但是我已经使用过护栏。 The js part should be entirely a string.
js部分应该完全是一个字符串。 Probably something like this:
大概是这样的:
<?php echo $ajax->link(substr($vehicle['vehicles']['year'], -2),
array('action' => 'add_mpc', 'category' => "$F('mpc_category')"),
array('update' => 'results', 'position' => 'top')); ?>
I'm assuming that it tacks "$key=$value" onto the params in the ajax link. 我假设它将“ $ key = $ value”添加到ajax链接中的参数上。 Also note you were missing a comma at the end of that second line.
另请注意,第二行末尾缺少逗号。
After working on this for a couple days now, the best I have come up with is this: 经过几天的努力,我想到的最好的是:
<?php echo $ajax->link($year,
array( 'action' => 'add_row',
'category' => $category,
'product' => $product.$newpart,
array( 'update' => $summary['summary']['id']." ".$vehicle['vehicles'],
'with' => "$('app_select').serialize()")); ?>
the 'with' => "$('app_select').serialize()"
being the part that grabs the values out of the form without having to submit the form. 'with' => "$('app_select').serialize()"
是无需提交表单即可从表单中获取值的部分。
I only needed one of the form elements, however, so this is not ideal as it passes the entire serialized form to the controller. 但是,我只需要一个表单元素,因此这并不理想,因为它将整个序列化的表单传递给了控制器。
I would still like to be able to do this with any element, regardless of it is in a form or not, but this method doesn't seem to do that. 我仍然希望能够使用任何元素来执行此操作,无论它是否以某种形式存在,但是此方法似乎都无法做到这一点。 Perhaps someone more familiar with prototype could shed some light on that.
也许更熟悉原型的人可以对此有所了解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.