我想把li元素放在一个数组中

时间:2016-04-06 15:30:46

标签: jquery html css arrays

我有一个可选的列表。我希望,例如,当我在该列表上选择3个项目时,然后单击确认按钮,将li元素添加到数组中。如果我选择其他项目,我也想将它们添加到该数组中。

$(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>

1 个答案:

答案 0 :(得分:0)

我不确定你在使用数组之后会怎么做,但你可以尝试这样的事情:

<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>
相关问题