簡體   English   中英

jQuery選擇所有子級

[英]jQuery select all children

我已經為用戶構建了一個小型應用程序來請求構建計算機。 我這樣做是通過在提交表單時將細節保存到localStorage中,然后循環訪問存儲變量以顯示請求的詳細外觀。此顯示部分循環遍歷本地存儲,並為ul附加li輸入與每台特定機器有關的每條信息。

以下是我的顯示在提交之前最終看起來像的粗略示例。 我需要選擇每個input的值,並在localStorage.Removeitem('all machine 1 input values go here');使用它localStorage.Removeitem('all machine 1 input values go here'); 我為粗略的代碼表示歉意,對於其中的大多數我還是陌生的。

<ul name="machine-1">
    <li><input type="button" value="Del"></li>
    <li><input value="name1"></li>
    <li><input value="number1"></li>
</ul>

<ul name="machine-2">
    <li><input type="button" value="Del"></li>
    <li><input value="name2"></li>
    <li><input value="number2"></li>
</ul>

這是構建列表的循環部分。

$("#theRequest").append('<ul id="machine-'+ machinec +'"><li style="border:none;margin-left:0em;padding-right:0em;"><input class="delete" type="button" id="del-'
    + machinec +'" value="Del"></li><li>'
    + '</li><li style="border:none;margin-left:0em;padding-right:0em;"><input class="edit" type="hidden" value="Edit"></li>'
    + '</li><li><input style="width:35px;" disabled type="text" value="'
    + nomember + '"></li><li><input style="width:20px;" disabled type="text" value="'
    + ecmpny + '"></li><li><input style="width:20px;" disabled type="text" value="'
    + estore + '"></li><li><input style="width:60px;" disabled type="text" value="'
    + hmachineType + '"></li><li><input disabled type="text" value="'
    + efullname + '"></li><li><input disabled type="text" value="'
    + etitle + '"></li><li><input disabled type="hidden" value="'
    + hrequested + '"></li><li><input disabled type="hidden" value="'
    + eemail + '</li></ul>');

這是我對如何“刪除”特定計算機並將其從localStorage中清除的猜測。 我基本上沒有選擇正確,如果我選擇了,我仍然認為它只會選擇孫輩價值觀之一?

// Delete a Machine
$('.delete').click(function() {
    if (confirm('Are you sure you want to remove this item?')) {
        var parent = $(this).parents('ul');
        var grandchildren = parent.children().children();
        parent.hide();
        inputName = grandchildren.attr('value');
        localStorage.removeItem(inputName);                     
    }                           
    else {}
});

我會嘗試這樣的

// Delete a Machine
$('.delete').click(function() {
    if (confirm('Are you sure you want to remove this item?')) {
        var parent = $(this).parents('ul').eq(0);  // just in case you have more than one ul ancestor
        parent.hide();
        parent.find('input').each(function() {
            localStorage.removeItem($(this).attr('value'));
        });                    
    }
});

順便說一句,如果您使用的是jQuery 1.7或更高版本,則可以使用$('.delete').on("click", function() {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM