2017-02-19 46 views
0

我得到了一點codepen,其中我有多個可選擇的選項卡(帶有jQuery可選小部件)。在上面,我得到了一個帶有文本輸入和提交按鈕的表單。當我插入一些文本並提交時,它會將文本添加到每個選項卡上的列表中。我的目標是輸入文本只會附加到選定選項卡的列表中。 那是我的javascript:僅將列表項添加到選定選項卡中的列表

$(function() { 
    function selectionChange(event, ui) { 
     var items = $('.ui-selected', this).map(function() { 
      return $(this).index(); 
     }).get(); 

     $("section").each(function() { 
      $(this).toggle(items.indexOf($(this).index()) > -1); 
     }); 
     console.log(items); 
    } 

    $("#selectable").selectable(); 
    $("#selectable").selectable({ 
     selected: selectionChange, 
     unselected: selectionChange 
    }); 
}); 


$(function(){ 
    $('#plannerform').submit(function(e){ 
     var val = $(this).find('#plannername').val(); 
     $('ul.plannerlist').append('<li>' + val + '</li>'); 
     e.preventDefault(); 
    }); 
}); 

並且那是HTML:

<form id="plannerform"> 
    <input id="plannername" placeholder="Name eingeben" type="text"></input><!-- 
    --><input id="plannersubmit" type="submit" value="eintragen"></input> 
</form> 

<ol id="selectable"> 
    <li class="ui-widget-content">FR PM</li> 
    <li class="ui-widget-content">SA AM</li> 
    <li class="ui-widget-content">SA PM</li> 
    <li class="ui-widget-content">SO AM</li> 
    <li class="ui-widget-content">SO PM</li> 
    <li class="ui-widget-content">MO AM</li> 
</ol> 

<div id="content"> 
    <section class="tabcontent">1 
    <ul class="plannerlist"> 
    </ul> 
    </section> 
    <section class="tabcontent">2 
    <ul class="plannerlist"> 
    </ul> 
    </section> 
    <section class="tabcontent">3 
    <ul class="plannerlist"> 
    </ul> 
    </section> 
    <section class="tabcontent">4 
    <ul class="plannerlist"> 
    </ul> 
    </section> 
    <section class="tabcontent">5 
    <ul class="plannerlist"> 
    </ul> 
    </section> 
    <section class="tabcontent">6 
    <ul class="plannerlist"> 
    </ul> 
    </section> 
</div> 

如果你需要更多的代碼,完整的代碼是我codepen我上面提到的。

回答

1

附加到$('.tabcontent:visible .plannerlist')相反,如果沒有一個是可見的(初始狀態),追加到一個在第一.tabcontent

這裏有一支筆,因爲表單提交將無法工作在SO http://codepen.io/mcoker/pen/YNoNdN

$(function() { 
 
    // define one function, to be used for both select/unselect 
 
    function selectionChange(event, ui) { 
 
     // Get indexes of selected items in an array 
 
     var items = $('.ui-selected', this).map(function() { 
 
      return $(this).index(); 
 
     }).get(); 
 
     // Show or hide sections according to the corresponding option's selection 
 
     $("section").each(function() { 
 
      $(this).toggle(items.indexOf($(this).index()) > -1); 
 
     }); 
 
     console.log(items); 
 
    } 
 

 
    $("#selectable").selectable(); 
 
    $("#selectable").selectable({ 
 
     selected: selectionChange, 
 
     unselected: selectionChange 
 
    }); 
 
}); 
 

 

 
$(function(){ 
 
    $('#plannerform').submit(function(e){ 
 
     var val = $(this).find('#plannername').val(); 
 
     var $target = $('.tabcontent:visible').length ? $('.tabcontent:visible .plannerlist') : $('.tabcontent:first-child .plannerlist'); 
 
     $target.append('<li>' + val + '</li>'); 
 
     e.preventDefault(); 
 
    }); 
 
});
*{ 
 
    font-family: 'Josefin Sans', sans-serif; 
 
    margin: 0; 
 
    padding: 0; 
 
    box-sizing: border-box; 
 
} 
 
#selectable .ui-selecting { 
 
    background: #9eefbc; 
 
    transition:.8s ease-in-out; 
 
    -webkit-transition: -webkit-transform 0.8s, background-color 0.8s; 
 
\t transition: transform 0.8s, background-color 0.8s; 
 
\t -webkit-transform: perspective(300px) rotate3d(1,0,0,-180deg); 
 
\t transform: perspective(300px) rotate3d(1,0,0,-180deg); 
 
\t -webkit-transform-origin: 50% 100%; 
 
\t transform-origin: 50% 100%; 
 
\t -webkit-perspective-origin: 50% 100%; 
 
\t perspective-origin: 50% 100%; 
 
} 
 
#selectable .ui-selected { 
 
    background: #6dce91; 
 
    transition:all 0.8s; 
 
} 
 
#selectable { 
 
    list-style-type: none; 
 
    position:absolute; 
 
    width: 60%; 
 
    margin-left:20%; 
 
    display:flex; 
 
    transition:.3s ease-in-out; 
 
    z-index:1; 
 
    margin-top:3px; 
 
} 
 
#selectable li { 
 
    background:#ddffea; 
 
    padding: 0.6em; 
 
    font-size: 1.4em; 
 
    flex-grow:1; 
 
    transition:.3s ease-in-out; 
 
    border:none; 
 
    text-align:center; 
 
    line-height:0.8em; 
 
} 
 

 
#selectable .ui-selected:after, 
 
#selectable .ui-selected::after { 
 
    position: absolute; 
 
    top: 44px; 
 
    margin-left:-50px; 
 
    transition: .2s ease-in-out; 
 
    content: ''; 
 
    width: 0; 
 
    height: 0; 
 
    opacity:1; 
 
    animation:dreieckFade 1s forwards; 
 
    border-top: solid 15px #6dce91; 
 
    border-left: solid 20px transparent; 
 
    border-right: solid 20px transparent; 
 
} 
 

 
@keyframes dreieckFade{ 
 
    0%{opacity:0;border-top: solid 0px #6dce91;} 
 
    100%{opacity:1;border-top: solid 15px #6dce91;} 
 
} 
 

 
.ui-selectable-helper { 
 
    visibility: hidden; 
 
} 
 

 
#content{ 
 
    width:60%; 
 
    background-color:#9eefbc; 
 
    margin-left:20%; 
 
    padding-top:70px; 
 
    margin-top:3px; 
 
    padding-bottom:30px; 
 
} 
 

 
.tabcontent{ 
 
    top:44px; 
 
    background-color:transparent; 
 
    z-index:0; 
 
    transition:.3s ease-in-out; 
 
    font-size:2em; 
 
    display:none; 
 
    padding-left:100px; 
 
} 
 

 
#plannername{ 
 
    width:40%; 
 
    background-color:#9eefbc; 
 
    margin-left:20%; 
 
    border:0; 
 
    font-size:2em; 
 
    padding:20px; 
 
} 
 
#plannername:focus{ 
 
    outline:0; 
 
} 
 
#plannersubmit{ 
 
    width:20%; 
 
    background-color:#6dce91; 
 
    border:0; 
 
    font-size:2em; 
 
    padding:20px; 
 
    transition:.2s ease-in-out; 
 
} 
 
#plannersubmit:hover{ 
 
    transition:.2s ease-in-out; 
 
    padding-left:40px; 
 
    cursor:pointer; 
 
} 
 
#plannersubmit:focus{ 
 
    outline:0; 
 
} 
 
#plannersubmit:active{ 
 
    color:white; 
 
} 
 
.plannerlist{ 
 
    list-style-type:none; 
 
}
<link href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" rel="stylesheet"/> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> 
 

 
<form id="plannerform"> 
 
    <input id="plannername" placeholder="Name eingeben" type="text" autocomplete="off"></input><!-- 
 
    --><input id="plannersubmit" type="submit" value="eintragen"></input> 
 
</form> 
 
<ol id="selectable"> 
 
    <li class="ui-widget-content">FR PM</li> 
 
    <li class="ui-widget-content">SA AM</li> 
 
    <li class="ui-widget-content">SA PM</li> 
 
    <li class="ui-widget-content">SO AM</li> 
 
    <li class="ui-widget-content">SO PM</li> 
 
    <li class="ui-widget-content">MO AM</li> 
 
</ol> 
 
<div id="content"> 
 
    <section class="tabcontent">1 
 
    <ul class="plannerlist"> 
 
    </ul> 
 
    </section> 
 
    <section class="tabcontent">2 
 
    <ul class="plannerlist"> 
 
    </ul> 
 
    </section> 
 
    <section class="tabcontent">3 
 
    <ul class="plannerlist"> 
 
    </ul> 
 
    </section> 
 
    <section class="tabcontent">4 
 
    <ul class="plannerlist"> 
 
    </ul> 
 
    </section> 
 
    <section class="tabcontent">5 
 
    <ul class="plannerlist"> 
 
    </ul> 
 
    </section> 
 
    <section class="tabcontent">6 
 
    <ul class="plannerlist"> 
 
    </ul> 
 
    </section> 
 
</div>

+0

啊,我不知道:可見。感謝那個簡單的解決方案。第二部分,該項目被追加到第一個列表上,當沒有選項卡被選中時,對我來說是非常不相關的,因爲我希望用戶先選擇一些標籤然後提交(我會用一些註釋來標識)。但是,謝謝你:)很高興知道。 –

+0

@TobiasGlaus np。在這種情況下,如果有人沒有選擇標籤(可能是壞的UX,因爲用戶可能不知道他們必須在搜索之前選擇標籤),或者默認選擇第一個標籤(似乎是更好的解決方案)。 –

+0

是的,它就像一個計劃者,你可以註冊幾天。所以我認爲禁用搜索輸入是最好的解決方案 –

0

你可以存儲在單擊處理程序選定索引,然後過濾那些出來的時候,你將數據追加:

$(function() { 
    //Create a variable to store the indices of selected items 
    var selectedIndices = []; 

    function selectionChange(event, ui) { 
     var items = $('.ui-selected', this).map(function() { 
      return $(this).index(); 
     }).get(); 

     $("section").each(function() { 
      $(this).toggle(items.indexOf($(this).index()) > -1); 
     }); 
     console.log(items); 
     //Store the indices so that they can be accessed when the plannerform gets submitted 
     selectedIndices = items; 
    } 

    $("#selectable").selectable(); 
    $("#selectable").selectable({ 
     selected: selectionChange, 
     unselected: selectionChange 
    }); 

    $('#plannerform').submit(function(e){ 
     var val = $(this).find('#plannername').val(); 
     //filter the list, leaving only the elements whose indices are present in the selectedIndices array 
     $('ul.plannerlist').filter(function(index) { 
      return selectedIndices.indexOf(index) > -1; 
     }).append('<li>' + val + '</li>'); 
     e.preventDefault(); 
    }); 
}); 
+0

感謝您的回答,但我想補充:可見像這樣'$('.tabcontent:visible .plannerlist')'是非常容易的。但我感謝你的幫助! –

相關問題