gpt4 book ai didi

jquery - 使用 JQuery 更新 span 标签值

转载 作者:技术小花猫 更新时间:2023-10-29 12:10:02 26 4
gpt4 key购买 nike

我正在尝试更新位于图例标记中的字段集标记中的跨度标记。这个想法是在选择组件时更新软件项目的成本。如果我只有一个软件项目(例如 - Visual Studio 2008 Pro $2800),下面的代码工作正常,但是如果我在另一个字段集中添加第二个项目,那么当我尝试更新该字段集的跨度时,它会更新跨度包含我的 Visual Studio 软件的字段集。知道我做错了什么吗?

<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("li").click(function() {
var itemCost = 0;
$("legend").each(function() {
var SoftwareItem = $(this).text();
itemCost = GetItemCost(SoftwareItem);
$("input:checked").each(function() {
var Component = $(this).next("label").text();
itemCost += GetItemCost(Component);
});
$("#ItemCostSpan").text("Item Cost = $ " + itemCost);
});
function GetItemCost(text) {
var start = 0;
start = text.lastIndexOf("$");
var textCost = text.substring(start + 1);
var itemCost = parseFloat(textCost);
return itemCost;
}
});
});
</script>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<fieldset>
<legend>Visual Studio 2008 Pro $2800</legend>
<ul class="AspNet-CheckBoxList-RepeatDirection-Vertical">
<li class="AspNet-CheckBoxList-Item">
<input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" value="first1" />
<label for="CheckBoxList1_0">Software Assurance $300.00</label>
</li>
<li class="AspNet-CheckBoxList-Item">
<input id="CheckBoxList1_1" type="checkbox" name="CheckBoxList1$1" value="second2" />
<label for="CheckBoxList1_1">Another Component $255.25</label>
</li>
<li class="AspNet-CheckBoxList-Item">
<input id="CheckBox1" type="checkbox" name="CheckBoxList1$1" value="second2" />
<label for="CheckBoxList1_1">A Second Component $15.25</label>
</li>
</ul>
<span id="ItemCostSpan" style="background-color:White"> </span>
</fieldset>
<fieldset>
<legend>Visio 2008 Pro $415</legend>
<ul class="AspNet-CheckBoxList-RepeatDirection-Vertical">
<li class="AspNet-CheckBoxList-Item">
<input id="CheckBox2" type="checkbox" name="CheckBoxList1$0" value="first1" />
<label for="CheckBoxList1_0">Software Assurance $40.00</label>
</li>
<li class="AspNet-CheckBoxList-Item">
<input id="CheckBox3" type="checkbox" name="CheckBoxList1$1" value="second2" />
<label for="CheckBoxList1_1">Another Component $25.55</label>
</li>
<li class="AspNet-CheckBoxList-Item">
<input id="CheckBox4" type="checkbox" name="CheckBoxList1$1" value="second2" />
<label for="CheckBoxList1_1">A Second Component $10.25</label>
</li>
</ul>
<span id="ItemCostSpan" style="background-color:White"></span>
</fieldset>

<span id="TotalCostSpan" style="background-color:White"></span>

</form>

最佳答案

标记 ID 必须是唯一的。您正在更新 ID 为“ItemCostSpan”的跨度,其中有两个。给 span 一个类并使用 find 获取它。

    $("legend").each(function() {
var SoftwareItem = $(this).text();
itemCost = GetItemCost(SoftwareItem);
$("input:checked").each(function() {
var Component = $(this).next("label").text();
itemCost += GetItemCost(Component);
});
$(this).find(".ItemCostSpan").text("Item Cost = $ " + itemCost);
});

关于jquery - 使用 JQuery 更新 span 标签值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/735359/

26 4 0