单击按钮时显示和隐藏 div,即使 php 中有 select 个选项

show and hide div on button click even if select options in php

我试图在点击按钮时显示 div。如果未单击该按钮,则应隐藏 div。该代码在这种情况下有效。但是,如果我 select 一个 div 形式的选项,即使我没有点击任何按钮,我也会重定向到旧页面。

如果我 select 任何选项然后它被重定向到 custom div。 如何解决?

更新:

我也在表单中使用了隐藏元素。我认为那是我的 problem.Anyone 帮助我解决了这个问题。

custom.phtml:

   <div class="content" id="custom">
    <input type="submit" name="create_custom" value="Create Custom" id="create_custom">
    <table id="custom" class="display" cellspacing="1" width="100%" >       

//...datatable
    </table>

</div>
<div class="content" id="test"  style="display:none">
    <?php 
     echo $this->render('/custom/index.phtml');
       ?>
</div>

Module.js :

$(document).ready(function(){
        $("#create_custom").click(function(){
            $("#custom").toggle(1000);
             $("#test").toggle(1000);
        });
    });

Index.phtml:

<?php echo $form; ?>

表格:

public function createElements(array $formData)
    {      
        $this->addElement(
            'note',
            'template',
            array(
                'order'         => 0,
                'value'         => $this->translate('Custom'),
                'decorators'    => array(
                    'ViewHelper',
                    array('HtmlTag', array('tag' => 'h3'))
                )
            )
        );
        if ( $this->filter_type == 'STATIC' ) {
                $this->addElement(
                    'multiselect',
                    'names',
                    array(
                        'order'         => 21,
                        'label' => $this->translate('Name'),
                        'required'      => true,
                        'value' => '',
                        'multiOptions' => array_unique( $this->name),
                        'description'   => $this->translate('Name'),
                        'autosubmit' => false
                    )
                 );
            } else {
                $text_type = (  $this->filter_type == 'MYSQL' )? 'textarea':'hidden';
                 $this->addElement(
                 $text_type,
                'format',
                array(
                    'order'         => 30,
                    'value'         => $this->query,
                    'label'         => $this->translate('Format'),
                    'description'   => $this->translate('Format'),
                    'required'      => true

                )
                );

                 if ( $text_type == 'hidden') {
                    $this->addElement(
                'note',
                'filter',
                array(
                    'order'         => 31,
                    'value'         => $this->filter,
                    'label'         => $this->translate('Format'),
                    'description'   => $this->translate('Format'),
                    'required'      => true    
                )
            );
                 }
    }

        $this->addElement(
            'select',
            'type',
            array(
                'order'         => 1,
                'label' => $this->translate('Type'),
                'required'      => true,
                'value' =>$this->filter_type,
                'multiOptions' => $this->getTypes(),
                'description'   => $this->translate('Type'),
                'autosubmit' => true
            )
        );
}

提前致谢。

您应该使用 Toggle,它可以用更少的代码帮助您。

您可以参考下面的示例进行切换演示。

$("button").click(function(){
    $("p").toggle(1000);
});