2016-04-06 5 views
0

Ich habe eine Liste, die auswählbar ist. Ich möchte zum Beispiel, wenn ich 3 Elemente auf dieser Liste auswählen, dann klicken Sie auf die Schaltfläche Bestätigen, um die li Elemente zu einem Array hinzuzufügen. Und wenn ich andere Elemente auswähle, möchte ich sie auch zu diesem Array hinzufügen.Ich möchte die li-Elemente in ein Array setzen

$(document).ready(function() { 
 
    $("#add").on("click", function(e) { 
 
    $("#list").append("<li>" + $("#prenume").val() + " " + $("#nume").val() + "</li>"); 
 
    return false; 
 
    }) 
 

 
    $(function() { 
 
    $("#list").selectable(); 
 
    }); 
 

 
    $("#confirm").on("click", function() { 
 
    }); 
 
});
#feedback { 
 
    font-size: 1.4em; 
 
} 
 
#list .ui-selecting { 
 
    background: #FECA40; 
 
} 
 
#list .ui-selected { 
 
    background: #F39814; 
 
    color: white; 
 
} 
 
#list { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    width: 20%; 
 
} 
 
#list li { 
 
    margin: 3px; 
 
    padding: 0.4em; 
 
    font-size: 1.4em; 
 
    height: 18px; 
 
} 
 
li:hover { 
 
    cursor: pointer; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    Prenume 
 
    <input type="text" id="prenume"></input> 
 
    Nume 
 
    <input type="text" id="nume"></input> 
 
    <select> 
 
    <option value="1">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
    <option value="4">4</option> 
 
    <option value="5">5</option> 
 
    </select> 
 
    <button id="add">Add</button> 
 
</form> 
 

 
<ul id="list"> 
 
</ul> 
 

 
<ul id="confirmed_list"> 
 
</ul> 
 
<button id="confirm">Confirm</button>

+0

Der Code funktioniert nicht, weil der jquery-ui. –

Antwort

0

Ich bin nicht sicher, was Sie suchen mit dem Array anschliessend zu tun, aber man könnte so etwas wie dies versucht:

<script> 
    $(document).ready(function() { 

     var tempArray = Array(); 

     $("#add").on("click", function (e) { 
      var prenume = $("#prenume").val(); 
      var nume = $("#nume").val(); 
      var both = prenume + " " + nume; 

      $("#list").append("<li>" + both + "</li>"); 
      tempArray += both; 
      return false; 
     }) 

      // This was not working so I commented it out for now 
      // $(function() { 
      // $("#list").selectable(); 
      // }); 


     $("#confirm").on("click", function() { 
      $("#confirmed_list").append(tempArray); 
      tempArray = ""; 
     }); 
    }); 
</script> 
Verwandte Themen