2015-12-16 63 views
1

我有以下代碼。 (形式是在開始隱藏)JQuery。隱藏一個div和另一個在第一個地方

HTML

<div1 id="js_sel1"> 
    <p> this is selct 1 </p> 
    <a class="js_showform" data-id="1" href="#">edit1</a> 
</div> 
<div2 id="js_sel2"> 
    <p> this is select 2 </p> 
    <a class="js_showform" data-id="2" href="#">edit2</a> 
</div> 
<div3 id="js_sel3"> 
    <p> this is select 3 </p> 
    <a class="js_showform" data-id="3" href="#">edit3</a> 
</div> 
<form id="js_form" class="js_hide_form"> 
    <p> this is my form </p> 
    <a class="js_hideform" href="#">save</a> 
</form> 

jQuery的

$(function() { 
    var par = $('.js_hide_form'); 
    $(par).hide(); 
    $('.showform').click(function(e) { 
     e.preventDefault(); 
     var hide_ele = "#js_sel" + $(this).data("id"); 
     var show_ele = "#js_form"; 
     $(hide_ele).hide; 
     $(show_ele).show; 
    }); 
    $('.hideform').click(function(e) { 
     e.preventDefault(); 
     var hide_ele = "#js_form"; 
     var show_ele = "#js_sel" + $(this).data("id"); 
     $(hide_ele).hide; 
     $(show_ele).show; 
    }); 
}); 

在啓動,form標籤是隱藏的。點擊任何編輯(如div2的編輯2),我希望特殊的div(ex div2)應該消失,表格應該出現在DIV的特定區域(這裏是div2)。

如果你能幫助我,我會非常感激。 TY :)

+4

'div2','div3'?你怎麼能這樣做?這不是有效的HTML! – void

+0

用您的代碼更新此https://jsfiddle.net/mohamedyousef1980/maspc1uo/ –

回答

0

糾正您的div並嘗試JS:

<div id="js_sel1"> 
     <p> this is selct 1 </p> 
     <a class="js_showform" data-id="1" href="#">edit1</a> 
    </div> 
    <div id="js_sel2"> 
     <p> this is select 2 </p> 
     <a class="js_showform" data-id="2" href="#">edit2</a> 
    </div> 
    <div id="js_sel3"> 
     <p> this is select 3 </p> 
     <a class="js_showform" data-id="3" href="#">edit3</a> 
    </div> 
    <form id="js_form" class="js_hide_form"> 
     <p> this is my form </p> 
     <a class="js_hideform" href="#">save</a> 
    </form> 

JS:

 $(function() { 
    $('div[id^="js_sel"] .js_showform').on('click',function(){ 

var parent = $(this).closest('div[id^="js_sel"]'); 

$('#js_form').clone().insertBefore(parent); 
parent.hide(); 
parent.prev().show(); 
}); 
$('body').on('click','.js_hide_form a',function(){ 
console.log(this); 
    $(this).parent().next().show(); 
    $(this).parent().hide(); 

}); 

}); 

https://jsfiddle.net/9h8r7akt/1/

或:

$(function() { 
    $('div[id^="js_sel"] .js_showform').on('click',function(){ 

var parent = $(this).closest('div[id^="js_sel"]'); 

$('#js_form').detach().insertBefore(parent); 
$('div[id^="js_sel"] ').show(); 
parent.hide(); 
parent.prev().show(); 
}); 
$('body').on('click','.js_hide_form a',function(){ 
console.log(this); 
    $('div[id^="js_sel"]').show(); 
    $(this).parent().hide(); 

}); 

}); 

https://jsfiddle.net/9h8r7akt/3/

+0

https://jsfiddle.net/dishab16/oe2sbpzk/ 這是我的jsfiddle鏈接,我複製相同的代碼,但它不工作。我甚至檢查了設置。有什麼想法嗎? –

+0

其工作對我來說,只是從底部隱藏表格 – madalinivascu

+0

https://jsfiddle.net/oe2sbpzk/1/ – madalinivascu

1

請看看這個運行的代碼片段:

$(function() { 
 
    var par = $('.js_hide_form'); 
 
    $(par).hide(); 
 
    $('.js_showform').click(function(e) { 
 
     e.preventDefault(); 
 
     var hide_ele = "#js_sel" + $(this).data("id"); 
 
     var show_ele = "#js_form"; 
 
     if($(show_ele).is(":visible")) 
 
     return; 
 
     
 
     $(".js_hideform").data("id",$(this).data("id")); 
 
     $(hide_ele).hide(); 
 
     $(hide_ele).after($(show_ele).detach()); 
 
     $(show_ele).show(); 
 
    }); 
 
    $('.js_hideform').click(function(e) { 
 
     e.preventDefault(); 
 
     var hide_ele = "#js_form"; 
 
     var show_ele = "#js_sel" + $(this).data("id"); 
 
     $(hide_ele).hide(); 
 
     $(show_ele).show(); 
 
     
 
     
 
     $("#js_sel3").after($(hide_ele).detach()); //js_sel3 is the id of element after which we want the form should go back 
 
     
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<div id="js_sel1"> 
 
    <p> this is selct 1 </p> 
 
    <a class="js_showform" data-id="1" href="#">edit1</a> 
 
</div> 
 
<div id="js_sel2"> 
 
    <p> this is select 2 </p> 
 
    <a class="js_showform" data-id="2" href="#">edit2</a> 
 
</div> 
 
<div id="js_sel3"> 
 
    <p> this is select 3 </p> 
 
    <a class="js_showform" data-id="3" href="#">edit3</a> 
 
</div> 
 
<form id="js_form" class="js_hide_form"> 
 
    <p> this is my form </p> 
 
    <a class="js_hideform" href="#">save</a> 
 
</form>

+0

謝謝:) 我已經瞭解了多少,_form_片段正在** show_ele * * 當我們保存它時,是否有可能回到原來的位置(在代碼中)? –