【问题标题】:How to hide/show form using jQuery in following scenario?如何在以下场景中使用 jQuery 隐藏/显示表单?
【发布时间】:2013-09-24 07:30:51
【问题描述】:

我的网站使用 Smarty、Php 和 Jquery。当页面加载时,不应显示以下表单。当用户点击下面给出的超链接时,它应该被显示出来,如果用户想再次隐藏表单,他将点击相同的超链接来隐藏上面的表单。简而言之,在表单加载时,不应显示表单,单击超链接时,如果表单打开,则应显示该表单,如果隐藏,则应将其关闭。你能帮我用jQuery实现这个吗?提前致谢。

<a class="reply" href="{$control_url}modules/enquiries/reply_to_enquiry.php?contact_id={$data.contact_id}&op=reply&from_date={$from_date}&to_date={$to_date}">Reply</a>

<form id="manage_reply_enquiry" name="manage_reply_enquiry" method="post" action="{$control_url}modules/enquiries/reply_to_enquiry.php" enctype="multipart/form-data">
    <ul>
        <li>
            <label>{'To Name'|signal_on_error:$error_msg:'contact_full_name'} :</label>
            <div class="form-element">
                <input type="text" name="contact_full_name" id="contact_full_name" {if $data.contact_full_name!=''} value="{$data.contact_full_name}" {else} value="{$contact_full_name|capitalize}" {/if} class="">
            </div>
        </li>
        <li>
            <label>{'To Email'|signal_on_error:$error_msg:'contact_email_id'} :</label>
            <div class="form-element">
                <input type="text" name="contact_email_id" id="contact_email_id" {if $data.contact_email_id !=''} value="{$data.contact_email_id}" {else} value="{$contact_email_id}" {/if} class="">
            </div>
        </li>
        <li>
            <label>{'Reply'|signal_on_error:$error_msg:'reply'} :</label>
            <div class="form-element">
                <textarea name="reply" id="reply" cols="60" rows="12">{if $error_msg!=""}{$data.reply}{/if}</textarea>
            </div>
        </li>
        <li>
            <label>{'Upload File'|signal_on_error:$error_msg:'reply_file_name'} :</label>
            <div class="form-element">
                <p class="uploadBtn"><input type="file" id="reply_file_name" name="reply_file_name" /></p>
                <div class="input-info"> <span class="required">Note* (Image size should be less then 1 mb and alowed format types are jpg, jpeg, gif, png, JPG, JPEG, GIF, PNG, doc, docx)</span></div>
            </div>
        </li>
        <input type="hidden" name="contact_id" value="{$data.contact_id}" />
        <input type="hidden" name="from_date" value="{$from_date}" />
        <input type="hidden" name="to_date" value="{$to_date}" />
        <input type="hidden" name="op" value="{$op}" />
        <li>
            <label></label>
            <div class="form-element">
                <input type="submit" name="submit" id="submit" class="c-btn" value="Send">
                <input type="button" name="cancel" id="cancel" class="c-btn" value="Cancel" onclick="javascript:window.location.href='{$control_url}modules/enquiries/view_contact_us.php?page={$page}&from_date={$from_date}&to_date={$to_date}'">
            </div>
        </li>                
    </ul>
</form>

【问题讨论】:

    标签: javascript jquery html css forms


    【解决方案1】:

    没有启用 JS 的用户怎么办? This Fiddle 可能是更完整的解决方案:

    HTML:

    <!-- Have the link anchored to the form so it still makes sense for text-only, speech-readers, or users with JS disabled -->
    <a class="reply" href="#manage_reply_enquiry">Reply</a>
    <form id="manage_reply_enquiry">
        <fieldset>
            <legend>I am Legend</legend>
            <input type="text" name="field" value="a form field" />
        </fieldset>
    </form>
    

    JS:

    //If JS is enabled add a class so we can hide the form ASAP (and only for JS enabled browsers)
        document.documentElement.className = 'js';
    
    //add the jQuery click/show/hide behaviours (or native JS if you prefer):
        $(document).ready(function(){
            $(".reply").click(function(){
                if($("#manage_reply_enquiry").is(":visible")){
                    $("#manage_reply_enquiry").hide();
                } else {
                    $("#manage_reply_enquiry").show();
                }
                //don't follow the link (optional, seen as the link is just an anchor)
                return false;
            });
        });
    

    CSS:

    .js #manage_reply_enquiry {
        display:none; /* hide for JS enabled browsers */
    }
    #manage_reply_enquiry {
        /* form styles here */
    }
    

    DEMO

    【讨论】:

      【解决方案2】:

      最初使用 css 隐藏表单:

      #manage_reply_enquiry { display: none; }
      

      然后将事件处理程序绑定到链接并切换可见性:

      $(function(){ //when the DOM is ready
      
          $('a.reply').click(function(){ //when clicking the link
              $('#manage_reply_enquiry ').toggle();  //toggles visibility
          });
      
      });
      

      【讨论】:

        【解决方案3】:
        <form id="manage_reply_enquiry" name="manage_reply_enquiry" method="post" action="{$control_url}modules/enquiries/reply_to_enquiry.php" enctype="multipart/form-data" style="display: none">
        

        在你的脚本中

        $(".reply:first").click(function() {
              $("#manage_reply_enquiry").toggle();
        })
        

        【讨论】:

          【解决方案4】:

          试试:

          HTML:

            <div id="someId" style="display: none;">
                <form id="manage_reply_enquiry" name="manage_reply_enquiry" method="post" action="     {$control_url}modules/enquiries/reply_to_enquiry.php" enctype="multipart/form-data">
                   //your normal code
                </form>
            </div>
          

          JAVASCRIPT:

            $(document).ready(function(){
               $(".reply").click(function(){
                  if($("#someId").css("display") == "none"){
                     $("#someId").show();
                  } else {
                     $("#someId").hide();
                  }
               });
            });
          

          希望有帮助!

          【讨论】:

            【解决方案5】:

            方法 hide()、show() 和 toggle 很昂贵(性能)。好方法是

            var el = $("#someId")
                 if (el.style.display=='none'){
                     el.style.display='block'
                 }else{
                     el.style.display='none'
                 }
            

            1) 你有一个反对的请求! 2)原生js速度快

            【讨论】:

              【解决方案6】:

              对于任何有同样问题的人:)

              $(document).ready(function() {
              
                          $("#manage_reply_enquiry").hide();
              
                          $(".reply").on({
                              click: function(){
                                  $("#manage_reply_enquiry").toggle();
                              }
                          });
              
                      });
              

              【讨论】:

              • 文档加载完成后,找到一个id为“manage_reply_enquiry”的元素,当点击class为“reply”的元素时隐藏,检查id为“manage_reply_enquiry”的元素是否为“manage_reply_enquiry”显示与否,如果不显示,则隐藏元素...
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-04-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多