我一直在为学校做这个项目。它是K-J等级的供应清单。每个年级都有#2支铅笔和其他各种类似的东西。当你在幼儿园选择一个项目(#2铅笔)并选择相同的项目说五年级,然后生成我不需要的列表#2铅笔出现两次,但只有一次有一个数量旁边的数字。
我确信它与if语句有关,但我仍然想要掌握它们。
这是jquery移动部分
<body>
<section class="header_default footer_default" data-role="page" id="setup" data-title="Setup">
<article data-role="content">
<div class="loaded"></div>
<p id="count"></p>
<form>
<fieldset data-role="controlgroup" id="hoursName">
<legend><strong>Kindergarten Supply List:</strong></legend>
<input type="checkbox" class="checkbox-group" name="checkboxsupply" id="1" value="(48 Quanity) #2 6-sided pencils (sharpened preferred)" >
<label for="1">(48 Quanity) #2 6-sided pencils (sharpened preferred)</label>
<input type="checkbox" class="checkbox-group" name="checkboxsupply" id="2" value="(2 Quanity) Red pencils">
<label for="2">(2 Quanity) Red Pencils</label>
<input type="checkbox" class="checkbox-group" name="checkboxsupply" id="3" value="(1 Quanity) Pack of pink erasers">
<label for="3">(1 Quanity) Pack of pink erasers</label>
<input type="checkbox" class="checkbox-group" name="checkboxsupply" id="4" value="(1 Quanity) Box of Kleenex (tissue)">
<label for="4">(1 Quanity) Box of Kleenex (tissue)</label>
<input type="checkbox" class="checkbox-group" name="checkboxsupply" id="5" value="(1 Quanity) Pack of pencil cap erasers">
<label for="5">(1 Quanity) Pack of pencil cap erasers</label>
</fieldset>
<fieldset data-role="controlgroup" id="hoursName">
<legend><strong>First Grade Supply List:</strong></legend>
<input type="checkbox" name="checkboxsupply" id="1" value="(48 Quanity) #2 6-sided pencils (sharpened preferred)" >
<label for="1">(48 Quanity) #2 6-sided pencils (sharpened preferred)</label>
<input type="checkbox" name="checkboxsupply" id="2" value="(2 Quanity) Red pencils">
<label for="2">(2 Quanity) Red Pencils</label>
<input type="checkbox" name="checkboxsupply" id="3" value="(1 Quanity) Pack of pink erasers">
<label for="3">(1 Quanity) Pack of pink erasers</label>
<input type="checkbox" name="checkboxsupply" id="4" value="(1 Quanity) Box of Kleenex (tissue)">
<label for="4">(1 Quanity) Box of Kleenex (tissue)</label>
<input type="checkbox" name="checkboxsupply" id="5" value="(1 Quanity) Pack of pencil cap erasers">
<label for="5">(1 Quanity) Pack of pencil cap erasers</label>
</fieldset>
<fieldset data-role="controlgroup" id="hoursName">
<legend><strong>Second Grade Supply List:</strong></legend>
<input type="checkbox" name="checkboxsupply" id="1" value="(48 Quanity) #2 6-sided pencils (sharpened preferred)" >
<label for="1">(48 Quanity) #2 6-sided pencils (sharpened preferred)</label>
<input type="checkbox" name="checkboxsupply" id="2" value="(2 Quanity) Red pencils">
<label for="2">(2 Quanity) Red Pencils</label>
<input type="checkbox" name="checkboxsupply" id="3" value="(1 Quanity) Pack of pink erasers">
<label for="3">(1 Quanity) Pack of pink erasers</label>
<input type="checkbox" name="checkboxsupply" id="4" value="(1 Quanity) Box of Kleenex (tissue)">
<label for="4">(1 Quanity) Box of Kleenex (tissue)</label>
<input type="checkbox" name="checkboxsupply" id="5" value="(1 Quanity) Pack of pencil cap erasers">
<label for="5">(1 Quanity) Pack of pencil cap erasers</label>
</fieldset>
<fieldset data-role="controlgroup" id="hoursName">
<legend><strong>Third Grade Supply List:</strong></legend>
<input type="checkbox" name="checkboxsupply" id="1" value="(48 Quanity) #2 6-sided pencils (sharpened preferred)" >
<label for="1">(48 Quanity) #2 6-sided pencils (sharpened preferred)</label>
<input type="checkbox" name="checkboxsupply" id="2" value="(2 Quanity) Red pencils">
<label for="2">(2 Quanity) Red Pencils</label>
<input type="checkbox" name="checkboxsupply" id="3" value="(1 Quanity) Pack of pink erasers">
<label for="3">(1 Quanity) Pack of pink erasers</label>
<input type="checkbox" name="checkboxsupply" id="4" value="(1 Quanity) Box of Kleenex (tissue)">
<label for="4">(1 Quanity) Box of Kleenex (tissue)</label>
<input type="checkbox" name="checkboxsupply" id="5" value="(1 Quanity) Pack of pencil cap erasers">
<label for="5">(1 Quanity) Pack of pencil cap erasers</label>
</fieldset>
<fieldset data-role="controlgroup" id="hoursName">
<legend><strong>Forth Grade Supply List:</strong></legend>
<input type="checkbox" name="checkboxsupply" id="1" value="(48 Quanity) #2 6-sided pencils (sharpened preferred)" >
<label for="1">(48 Quanity) #2 6-sided pencils (sharpened preferred)</label>
<input type="checkbox" name="checkboxsupply" id="2" value="(2 Quanity) Red pencils">
<label for="2">(2 Quanity) Red Pencils</label>
<input type="checkbox" name="checkboxsupply" id="3" value="(1 Quanity) Pack of pink erasers">
<label for="3">(1 Quanity) Pack of pink erasers</label>
<input type="checkbox" name="checkboxsupply" id="4" value="(1 Quanity) Box of Kleenex (tissue)">
<label for="4">(1 Quanity) Box of Kleenex (tissue)</label>
<input type="checkbox" name="checkboxsupply" id="5" value="(1 Quanity) Pack of pencil cap erasers">
<label for="5">(1 Quanity) Pack of pencil cap erasers</label>
</fieldset>
<fieldset data-role="controlgroup" id="hoursName">
<legend><strong>Fifth Grade Supply List:</strong></legend>
<input type="checkbox" name="checkboxsupply" id="1" value="(48 Quanity) #2 6-sided pencils (sharpened preferred)" >
<label for="1">(48 Quanity) #2 6-sided pencils (sharpened preferred)</label>
<input type="checkbox" name="checkboxsupply" id="2" value="(2 Quanity) Red pencils">
<label for="2">(2 Quanity) Red Pencils</label>
<input type="checkbox" name="checkboxsupply" id="3" value="(1 Quanity) Pack of pink erasers">
<label for="3">(1 Quanity) Pack of pink erasers</label>
<input type="checkbox" name="checkboxsupply" id="4" value="(1 Quanity) Box of Kleenex (tissue)">
<label for="4">(1 Quanity) Box of Kleenex (tissue)</label>
<input type="checkbox" name="checkboxsupply" id="5" value="(1 Quanity) Pack of pencil cap erasers">
<label for="5">(1 Quanity) Pack of pencil cap erasers</label>
</fieldset>
<fieldset data-role="controlgroup" id="hoursName">
<legend><strong>Sixth Grade Supply List:</strong></legend>
<input type="checkbox" name="checkboxsupply" id="1" value="(48 Quanity) #2 6-sided pencils (sharpened preferred)" >
<label for="1">(48 Quanity) #2 6-sided pencils (sharpened preferred)</label>
<input type="checkbox" name="checkboxsupply" id="2" value="(2 Quanity) Red pencils">
<label for="2">(2 Quanity) Red Pencils</label>
<input type="checkbox" name="checkboxsupply" id="3" value="(1 Quanity) Pack of pink erasers">
<label for="3">(1 Quanity) Pack of pink erasers</label>
<input type="checkbox" name="checkboxsupply" id="4" value="(1 Quanity) Box of Kleenex (tissue)">
<label for="4">(1 Quanity) Box of Kleenex (tissue)</label>
<input type="checkbox" name="checkboxsupply" id="5" value="(1 Quanity) Pack of pencil cap erasers">
<label for="5">(1 Quanity) Pack of pencil cap erasers</label>
</fieldset>
</form>
<button class="ui-btn ui-corner-all Create">Create List</button>
</article><!--content-->
</section>
这是我的javascript
<div data-role="page" id="nameList"></div>
<script id="jsbin-javascript">
$(".Create").on("click", function() {
var selectedValues = "";
$checkedCheckboxes = $("input:checkbox[name=checkboxsupply]:checked");
$checkedCheckboxes.each(function () {
$('#nameList').append('<li>' + $(this).val() + '</li>');
});
window.location = "#nameList";
});
</script>
任何建议都会很棒,我尝试使用jquery作为计算数量但没有运气的方法。
再次感谢。
答案 0 :(得分:0)
假设您无法更改任何标记,这可能是一种有点hacky的方式:)
$(".Create").on("click", function() {
var valuesToListItemMap = {};
$('#nameList').text("");
$checkedCheckboxes = $("input:checkbox[name=checkboxsupply]:checked");
$checkedCheckboxes.each(function () {
var value = $(this).val();
var element;
if (valuesToListItemMap[value]) {
element = valuesToListItemMap[value];
var newCount = parseInt(element.data('count')) + 1;
element.data('count', newCount);
element.text(value + ' ' + newCount + ' were selected');
} else {
element = $('<li>' + value + '</li>');
element.data('count', 1);
$('#nameList').append(element);
valuesToListItemMap[value] = element;
}
});
window.location = "#nameList";
});