简体   繁体   English

jQuery - 如何从两个不同的事件加载公共 div 中的内容?

[英]jQuery - how to load content in a common div from two differnet events?

I am not a JS ninja hence I am asking for some guidance here.我不是 JS 忍者,因此我在这里寻求一些指导。 I have following scenario我有以下情况

  1. page loads and commondiv is hidden页面加载和 commondiv 被隐藏
  2. if user clicks button showeditform, I load editform in commondiv and show the commondiv如果用户点击按钮showitform,我在commondiv中加载editform并显示commondiv
  3. if user clicks button showeditform and editform is visible, remove it and hide commondiv如果用户单击按钮showitform 并且editform 可见,请将其删除并隐藏commondiv
  4. if user clicks button showpasswordform and if the editform is visible and I remove editform and show passwordform如果用户单击按钮 showpasswordform 并且编辑表单可见并且我删除编辑表单并显示密码表单
  5. if user clicks button showpasswordform and if the passwordform is visible, remove it and hide the common div如果用户单击按钮 showpasswordform 并且密码表单可见,则将其删除并隐藏公共 div
  6. if user clicks button showeditform and if the passwordform is visible and I remove passwordform and show editform如果用户单击按钮显示itform 并且密码表单可见并且我删除密码表单并显示编辑表单

As of now, I have set up flags and if elses but its not very good way to do it.到目前为止,我已经设置了标志和 if elses,但这不是很好的方法。 How can I achieve this using minimum of jQuery code?如何使用最少的 jQuery 代码来实现这一目标?

Update : Following is my attempt更新:以下是我的尝试

$('a.editpo, a.resetpass').click(function(event){
    event.preventDefault();
    var urlToCall = $(this).attr('href');
    var hyperlinkid = '#'+$(this).attr('id');
    var targetId = $(this).attr('id').match(/\d+/);
    var targetTrDiv = '#poformloadertr_'+targetId;
    var targetTdDiv = '#poformloadertd_'+targetId;
    var toToggle = $('#resetuserpassform_'+targetId).is(':visible') || $('#account-home-container-'+targetId).is(':visible') ;
    console.log(toToggle);
    if(toToggle == true || $(targetTdDiv).html()==''){
        $.ajax({
            url:urlToCall,
            success: function(html){
                $(targetTdDiv).html(html);
                $(targetTrDiv).show();
            }
        });
    }else{
        $(targetTrDiv).hide();
        $(targetTdDiv).html('');
    }

});

The editpo and resetpass are classes applied on hyperlinks in column of table, namely Edit personal info and reset pass, clicking these load up the form in a tr>td . editporesetpass是应用于表格列中超链接的类,即 Edit personal info 和 reset pass,单击这些在tr>td中加载表单。 As I said, am not good at JS and specially jQuery, hence if you feel that code can be optimised at some step please feel free to do so.正如我所说,我不擅长 JS,特别是 jQuery,因此如果您觉得代码可以在某个步骤进行优化,请随时这样做。 Thanks!谢谢!

Try this尝试这个

    $(function(){

        var $commonDiv = $("#commondiv");

        //Add the edit and password forms into common div and hide them initially
        $("#editform").hide().appendTo($commonDiv);
        $("#passwordform").hide().appendTo($commonDiv);

//Editing answer based on your comments.

        $(".showeditform").live('click', function(){

            if($("#editform").hasClass("loading")){//If form is already loading just return
               returnl
            }

            if(!$("#editform").is(":visible")){
               $("#editform").addClass("loading").load("EditFormUrl", function(){
                   $(this).removeClass("loading").show();
               });  
            }
            else{
               $("#editform").hide();
            }

            //Always hide the passwordform
            $("#passwordform").hide();
        }); 

       $(".showpasswordform").live('click', function(){

            if(!$("#passwordform").is(":visible")){
               $("#passwordform").addClass("loading").load("PasswordFormUrl", function(){
                   $(this).removeClass("loading").show();
               });  
            }
            else{
               $("#passwordform").hide();
            }

            //Always hide the editform
            $("#editform").hide();

        });
    });

I have done this like a week ago, but the code is at home, where I am not at right now.一周前我已经这样做了,但是代码在家里,我现在不在。 But basically, you give your forms names in a array, and hide the ones that are not selected and if the one that is selected is clicked again use .toggle();但基本上,您将 forms 名称放在一个数组中,并隐藏未选择的名称,如果再次单击已选择的名称,请使用.toggle(); function to hide it. function 隐藏它。

You can give this a try.你可以试试这个。 This is assuming you are storing your form HTML in JS.这是假设您将表单 HTML 存储在 JS 中。 If you are storing it in a hidden container I can show you how to move that into the new parent or just copy the html into it.. hope this helps!如果您将其存储在隐藏容器中,我可以向您展示如何将其移动到新的父容器中,或者只是将 html 复制到其中。希望这会有所帮助!

$("#commondiv").hide();

$("#showeditform").click(function(){
  if($("#commondiv").is(":visible")) {
    $("#commondiv").html("").hide();
  } else {
    $("#commondiv").html(editformHTML).show();
  }
});
$("#showpasswordform").click(function(){
  if($("#commondiv").is(":visible")) {
    $("#commondiv").html("").hide();
  } else {
    $("#commondiv").html(passwordformHTML).show();
  }
});

You would do something like this:你会做这样的事情:

  $('#showeditform').click(function()
  {
       if($('#commondiv').is(':visible'))
          $('#commondiv').hide();
       else 
          $('#commondiv').html('The text you want to set').show();

       if($('#passwordform').is(':visible'))
       {
           $('#passwordform').remove();
           $('#editform').show();
       }
  });

  $('#showpasswordform').click(function()
  {
      if($('#editform').is(':visible'))
      {
         $('#editform').hide();
         $('#passwordform').show();
      }

      if($('#passwordform').is(':visible'))
      {
         $('#passwordform').remove();
         $('#commondiv').hide();
      }
  });

Honestly, it sounds like your page is doing far too much on a single page.老实说,听起来您的页面在单个页面上做得太多了。 The rules shown above give me a kind of a headache.上面显示的规则让我有点头疼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM