javascript - 即使在 php 中选择选项,也会在按钮单击时显示和隐藏 div

标签 javascript php ajax zend-framework

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

如果我选择任何选项,则会将其重定向到自定义 div。 怎么解决这个问题?

更新:

我也在表单中使用了隐藏元素。我认为这是我的问题。任何人都可以帮助我解决这个问题。

自定义.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>

模块.js:

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

索引.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);
});

关于javascript - 即使在 php 中选择选项,也会在按钮单击时显示和隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48749123/

相关文章:

javascript - 逻辑或代替三元

php - 使用php mysql动态显示图像

php - 如何根据用户输入创建链接

jquery - jsonp 没有在发送之前触发?

jquery - AJAX/JQuery - var 不存在

javascript - Vue : how to merge two reactive objects without loosing reactivity

javascript - 使用空字符串、null 或 undefined 调用的号码

javascript - JW Player 无法播放 DAILYMOTION 和 VIMEO 视频,但 YOUTUBE 视频可以正常播放

php - 使用 PHP 枚举 MySQL DB 中的多个单元格

jquery - jQuery Ajax与浏览器URL