gpt4 book ai didi

javascript - 在 javaScript 中使用 $(this) 而不是 ID

转载 作者:行者123 更新时间:2023-11-30 08:31:39 25 4
gpt4 key购买 nike

是否有任何解决方案可以使用 $(this) 而不是 javaScript 中的选项选择和 ID?

 var tot = 5 * ($( "#firstOne option:selected" ).text());

在上面的示例中,我想使用 $(this) 而不是 #firstOne,以便能够使用此函数进行多选项选择。

更新

jsFiddle

$(document).ready(function() {

document.getElementById("totalPrice").innerHTML = 0 + " €";
});

function addToPrice() {
var tot = $("#totalPrice").val();
tot = 5 * ($(this).find("option:selected").text());
tot = tot + " €"
document.getElementById("totalPrice").innerHTML = tot;

}
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>

<div>

<table class="itemList">
<tr>
<td style="width: 170px">
<span>Arancina Pistacchio</span>
</td>
<td>
<span style="margin-left: 110px;">2.5 &euro;</span>
</td>
<td>
<select style="margin-left: 30px" onChange="addToPrice();">
<option value="one">0</option>
<option value="two">1</option>
<option value="three">2</option>
<option value="four">3</option>
</select>
</td>
</tr>
</table>

</div>

<div>

<table class="itemList">
<tr>
<td style="width: 170px">
<span>Arancina Melanzana</span>
</td>
<td>
<span style="margin-left: 110px;">2.5 &euro;</span>
</td>
<td>
<select style="margin-left: 30px">
<option value="one">0</option>
<option value="two">1</option>
<option value="three">2</option>
<option value="four">3</option>
</select>
</td>
</tr>
</table>

</div>

<div class="totPrice">Total :</div>
<div class="totPrice" style="font-size: 20px; font-family: Helvetica" id="totalPrice"></div>

最佳答案

我明白你的意思了。使用如下:

var tot = 5 ;
$( "option:selected" ).each(function(){
tot = tot * $(this).text();
});

关于javascript - 在 javaScript 中使用 $(this) 而不是 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37048079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com