如何在表单中使用JavaScript? [英] How to use JavaScript in forms?

查看:169
本文介绍了如何在表单中使用JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将自己的JavaScript添加到由Doctrine生成的表单中?

例如,我可以在由symfony生成的输入表单中添加脚本onFocus ?

编辑:

@Benoit:



我尝试了你的方法,但它不起作用。

我的函数renderJavascript:

  public function renderJavascript(){
$(function(){
$('。form')。focus(function(){this。 blur()});
});

我已经覆盖了render方法,但是没有错误,但它什么都不做。



我认为这是一个小错误,但我从symfony开始,我不明白所有的形式。

解决方案

您可以将html属性传递给表单上的render方法:

  <?php echo $ form [field_name]  - > render(array(onfocus=>alert('focused!');))?> 

您还可以在选项数组中设置class属性,并使用jQuery将javascript添加到字段。


How to add your own JavaScript to the forms which are generate by Doctrine ?

For example, where can I add a script onFocus in an input form generated by symfony ?

Edit :

@Benoit :

I have tried your method but, it doesn't work.

My function renderJavascript :

public function renderJavascript() {
  $(function() {
    $('.form').focus(function(){this.blur()});
});

And I have override the method render as you show me but there is no error but it does nothing.

I think it is a little error but I begin with symfony and I don't understand all in the forms.

解决方案

You can pass html attributes to the render method on the form:

<?php echo $form["field_name"]->render(array("onfocus" => "alert('focused!');")) ?>

You could also set "class" attribute in the options array and use jQuery to add javascript to the field.

这篇关于如何在表单中使用JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆