简体   繁体   English

有2个单词字符串时按空格拆分数组

[英]Splitting array by spaces when there are 2 word strings

I am dynamically adding and removing checkboxes from a user created list. 我正在动态添加和删除用户创建的列表中的复选框。 This works fine if the item put on the list is one word, but if it contains a space then it does not work. 如果列表中的项目是一个单词,则此方法很好,但是如果包含空格,则该项目不起作用。 I understand why it wouldn't work but can't find how to do successfully splice the correct one from the array. 我知道为什么它不起作用,但是找不到如何成功地从阵列中拼接正确的片段的原因。


var boxCounter=0;
function addToCheckListNew(item){
    var newSet = '<fieldset data-role="controlgroup" class="cbGroup' + boxCounter + '"></fieldset>';
    $('.cbDiv').append(newSet);
    var newBox = '<input type="checkbox" name="checkbox-' + boxCounter + '" id="checkbox-' + boxCounter + '" class="custom" /> <label for="checkbox-'+ boxCounter + '">'+item+'</label>';
    $(".cbGroup" + boxCounter).append(newBox).trigger('create');
    boxCounter++;
    $("#new-list-page-iteminput").val("");
    $("#new-list-page-itemlist").listview("refresh");
    currentList.push(item);
}

//Delete the selected checkboxes
function deleteCheckedListNew(){
    var checkedBoxes = $('form input:checked').parents('fieldset');
    for (var i=0;i<checkedBoxes.length;i++){
        var str = (checkedBoxes.text().trim());
            str = str.split(" ");
        var itemIndex = $.inArray(str[i],currentList);
        if(itemIndex!==-1){
            currentList.splice(itemIndex,1);
            checkedBoxes.remove();
        }
    }
}

The items put into the checkboxes are also stored in an array currentList, it is this that I am trying to remove them from. 放入复选框的项目也存储在数组currentList中,这就是我要从中删除的项目。 Removing the checkedboxes is fine. 删除复选框很好。

I don't think you need to worry about the value of the text at all. 我认为您根本不需要担心文本的价值。

You can simply use the display index of the items to delete from the stored index. 您可以简单地使用项目的显示索引从存储的索引中删除。

This is on the assumption that you won't have any sorting built in yet 这是基于您尚未内置任何排序的假设

I simplified this down to 我简化为

//Delete the selected checkboxes
function deleteCheckedListNew() {
    var checkedRows = $('fieldset').has(':checkbox:checked');
    checkedRows.each(function(index){
        currentList.splice(index, 1)
    }).remove();

}

The next step I would suggest would be to use an array of objects for each item. 我建议的下一步是为每个项目使用对象数组。 Then you can assign a unique id to each which is far simpler to work with than parsing text. 然后,您可以为每个ID分配一个唯一的ID,这比解析文本要容易得多。

Having an ID stored would then let you sort the list and still easily find the stored object using array filter() or other methods. 存储ID后,您便可以对列表进行排序,并且仍然可以使用数组filter()或其他方法轻松地找到存储的对象。

The array would then look more like 该数组将看起来更像

[
  {id:1, text:'eat lunch'},
  {id:2, text:'have a beer'}
]

The id would then also be put onto each row in the template making it easy to identify in array 然后将id放在模板的每一行中,从而易于在数组中识别

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM