是否可以在 Zend 表单中显示和隐藏子表单 onclick [英] Is it possible to show and hide sub form onclick in a Zend form

查看:41
本文介绍了是否可以在 Zend 表单中显示和隐藏子表单 onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是否可以在单选检查事件或按钮 onclick 事件中显示和隐藏 Zend 表单中的子表单.因为我有一个带有用户字段元素的表单,现在我想要一个带有密码元素的子表单,这将使用户能够选择更改他们的密码.但是,我只想根据要求显示密码元素(即:单击单选按钮更改密码"并显示更改密码元素).

Just wondering whether is it possible to show and hide a subform within a zend form on either a radio check event or button onclick event. As I have a form with user field elements and now I want a sub form with password elements which will give the user the ability to optionally change their password. However I only want to show the password elements on request (ie: click a radio button 'Change Password' and the change password elements appear).

这可以用 Zend\Form 还是我需要使用客户端 javascript 来显示和隐藏元素?

Is this possible with Zend\Form or would I need to use client side javascript to show and hide the elements?

推荐答案

这是可能的,但这种事情是客户端,所以你需要使用 javascript 才能做到这一点.个人而言,我喜欢用 jQuery 来处理这类事情,它让它变得容易多了.这是一个关于如何做到这一点的示例.

It is possible but that kind of thing is client side so you need to use javascript in order to do it. Personnaly, I like to use jQuery for that kind of stuff, it makes it a lot easier. Here is an example on how you could do it.

class My_Form extends Zend_Form {

$field = $this->createElement('select', 'myselect');
$field->setLabel('Choose to display the form or not');
$field->setMultiOptions(array('1'='Display', '2'=>'Do not display'));
$this->addElement($field);

$field = $this->createElement('text', 'optionaltext');
$field->setLabel('This is an optional field');
$this->addElement($fiel);
}

现在,在您的布局中,您应该包含 jQuery 库:

Now, in your layout, you should include jQuery library:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">

最后,您应该包含另一个 .js 文件(或简单地将代码嵌入页面的 <script> 标签中).

And finally, you should include another .js file (or simply embed the code in <script> tags on your page).

$(document).ready(function() {
 $(function() {
  //Function triggered when changing the value in the drop down
  $('#myselect').change(function(event) {
   if($('#myselect').val() == 1) {
    //Show elements
    $('#optionaltext').show();
    //The following line shows/hides all the dd/dt wrappers as well
    $('[id*=optionaltext]).show();
   } else {
    //Hide elements
    $('#optionaltext').hide();
    //The following line shows/hides all the dd/dt wrappers as well
    $('[id*=optionaltext]).hide();
   }
  });
 });
});

现在请记住,我还没有测试代码,我只是在我真正喝完当天的第一杯咖啡之前把它写在我的头上,所以......它可能有一些错误.话虽如此,这应该是您想做的事情的良好开端.如果有什么遗漏或您找不到的错误,请在此处提出您的问题.希望这会有所帮助!

Now please keep in mind that I haven't tested the code and I just wrote that on top of my head before I actually finished my first coffee of the day so... it might have a few bugs. This being said, it should be a good start for what you want to do. Please just ask your questions here if there is something missing or if there is a bug you can't find. Hope this helps !

这篇关于是否可以在 Zend 表单中显示和隐藏子表单 onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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