2016-11-22 48 views
0

我試圖在我的選擇框中選中某個選項時獲得一個事件,即使該選項已被選中。原因是,我想將值添加到我的列表中,並有可能多次添加它們。當我再次選擇相同的選項時,我該如何觸發事件?

現在我可以添加dog,然後cat然後dog。所以在我的名單上有

  • 但它不可能cat後立即加入cat,所以我的列表看起來像這樣:

  • $(function() { 
     
        //Initialize Select2 Elements 
     
        $(".select2").select2(); 
     
    }); 
     
    
     
    $(document).ready(function() { 
     
        $(".select2").on("change", function() { 
     
        var classname = $(".select2 option:selected").val(); 
     
        $(".result").append("<li>" + classname + "</li>"); 
     
        }); 
     
    });
    <link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet"/> 
     
    <link href="https://select2.github.io/css/bootstrap.css" rel="stylesheet"/> 
     
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     
    <script src="https://select2.github.io/vendor/js/bootstrap.min.js"></script> 
     
    <script src="https://select2.github.io/dist/js/select2.full.js"></script> 
     
    
     
    <div class="form-group"> 
     
        <select class=" form-control select2" style="width: 100%;"> 
     
        <option>Cat</option> 
     
        <option>Dog</option> 
     
        <option>Bird</option> 
     
        </select> 
     
    </div> 
     
    
     
    
     
    
     
    <div class="result"></div>

    編輯: 我發現我工作的解決方案:

    $(function() { 
     
        //Initialize Select2 Elements 
     
        $(".select2").select2(); 
     
    }); 
     
    
     
    $(document).ready(function() { 
     
        $(".select2").on("change", function() { 
     
        var classname = $(".select2 option:selected").val(); 
     
        $(".result").append("<li>" + classname + "</li>"); 
     
        var should_be_selected = 'empty'; 
     
        $(".select2 option:selected").removeAttr("selected"); 
     
        $('.select2').find('.' + should_be_selected).attr('selected', 'selected'); 
     
        
     
        }); 
     
    });
    <link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet"/> 
     
    <link href="https://select2.github.io/css/bootstrap.css" rel="stylesheet"/> 
     
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     
    <script src="https://select2.github.io/vendor/js/bootstrap.min.js"></script> 
     
    <script src="https://select2.github.io/dist/js/select2.full.js"></script> 
     
    
     
    <div class="form-group"> 
     
        <select class=" form-control select2" style="width: 100%;"> 
     
        <option class="empty"></option> 
     
        <option>Cat</option> 
     
        <option>Dog</option> 
     
        <option>Bird</option> 
     
        </select> 
     
    </div> 
     
    
     
    
     
    
     
    
     
    <div class="result"></div>

    +1

    添加一個按鈕旁邊的'選擇'當單擊按鈕時將選定值附加到列表 –

    +1

    您正在使用對選擇標籤的更改!不是爲了選擇!因此只有當您更改該值時纔會觸發您的功能!嘗試第一次選擇「貓」! –

    +0

    好主意!我也考慮過在選擇更改後取消選擇所有選項 – Jarla

    回答

    1

    change事件僅觸發時所選擇的選項變化 - 正如其名稱所暗示。如果您希望在選擇相同選項時激發此行爲,則沒有可靠的解決方法。

    另一種是將DOM中的<button>,然後只將新li單擊該按鈕時,是這樣的:

    $(function() { 
     
        $(".select2").select2(); 
     
    
     
        $(".add").on("click", function() { 
     
        var classname = $(".select2").val(); 
     
        $(".result").append("<li>" + classname + "</li>"); 
     
        }); 
     
    });
    .remove { 
     
        cursor: pointer 
     
    }
    <link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet" /> 
     
    <link href="https://select2.github.io/css/bootstrap.css" rel="stylesheet" /> 
     
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     
    <script src="https://select2.github.io/vendor/js/bootstrap.min.js"></script> 
     
    <script src="https://select2.github.io/dist/js/select2.full.js"></script> 
     
    
     
    <div class="form-group"> 
     
        <select class=" form-control select2" style="width: 100%;"> 
     
        <option>Cat</option> 
     
        <option>Dog</option> 
     
        <option>Bird</option> 
     
        </select> 
     
        <button class="add">Add</button> 
     
    </div> 
     
    <div class="result"></div>

    +0

    非常感謝!但我需要一個沒有額外按鈕的解決方案 – Jarla

    +0

    您是否還應該包含'change'事件?如果我使用我的鍵盤進行選擇並按Enter鍵進行選擇,該怎麼辦? @RoryMcCrossan – Roberrrt

    +0

    我發現了一個解決方案,爲我工作,看到我的問題 – Jarla

    相關問題