在JS Helper CakePHP中手动触发事件 [英] Triggering event manually in JS Helper CakePHP
问题描述
我呆在一个情况。我使用JS助手。我使用以下代码。
I stuck in a situation. I am using JS Helper. I used the following code.
<?php $this->Js->get('#client_id')
->event('change', $this->Js->request(array('action' => '../ajax/get_client_location_and_process'),
array('update' => '#client_location_process',
'async' => false,
'dataExpression' => true,
'method' => 'post',
'evalScripts' => true,
'data' => $this->Js->serializeForm(array('isForm' => True, 'inline' => True))
)
)
);
我想在页面加载时触发更改事件。如果我使用document.ready方法,那么它是不工作。我找不到JS Helper方法,我们可以显式地触发控件上的一些事件。
I want to trigger the change event on page load. If I am using document.ready method then it is not working. and I unable to find the JS Helper method where we can explicitly trigger some event on controls. Please suggest the code how can I perform JQuery trigger() like functionality on form elements whenever I need it.
推荐答案
由于你已经在使用JQuery在jQuery中发现了 .trigger()
,您只需将其与您的视图代码一起使用:
Since you already found out about .trigger()
in jQuery, you can just use it along with your view code:
<?php
// Your view code
?>
<script>$('#client_id').trigger('change');</script>
或者,如果你还是喜欢通过PHP来做,你可以做自己的助手,例如。
Alternatively, if you still prefer to do it via PHP, you can perhaps make your own helper, eg.:
<?php
class ArunjsHelper extends AppHelper {
public $helpers = array('Html');
function trigger($element, $event, $options = array()) {
return $this->Html->scriptBlock("$('$element').trigger('$event');");
}
}
添加 ArunjsHelper
到 $ helpers
在控制器:
<?php
class SomeController extends Controller {
public $helpers = array('Arunjs');
// Your controller code
}
从视图中调用它:
<h1>Hello</h1>
<p>Your usual view HTML code</p>
<?php // Trigger the change event ?>
<?php echo $this->Arunjs->trigger('#client_id', 'change'); ?>
这篇关于在JS Helper CakePHP中手动触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!