gpt4 book ai didi

javascript - 如何在具有不同 ID 的多个 Select 元素上使用相同的 JavaScript 函数

转载 作者:行者123 更新时间:2023-11-30 17:30:48 28 4
gpt4 key购买 nike

我有这些 JavaScript 函数,我需要将它们用于 4 个选择元素,每个元素都使用唯一的 ID。

1) 我怎样才能使用这些函数使所有 4 个选择下拉菜单功能正确?

这里有 2 个 JavaScript 函数和 4 个 html 选择下拉菜单供引用。

第一个脚本

$(document).ready(function () {
$(".addToCart").on("click", function () {
var quant = $("#getquantity");
console.log(quant.val());
if (!quant.val()) {
alert("Select Quantity!");
} else {
var productCode = quant.val();
window.location.href = "url.com/2.php?a=" + productCode + "&d=" + id + "&c=US&durl=" + idname;
}
});
});

第二个脚本

function select_options() {
var textBlocks = [
'<font class="font-color-grey">Retail Price: <s>$45.00</s></font><br>Sales Price: <font class="font-color-red">$35.00</font>',
'<font class="font-color-grey">Retail Price: <s>$45.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$35.00</font></font>',
'<font class="font-color-grey">Retail Price: <s>$90.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$70.00</font></font>',
'<font class="font-color-grey">Retail Price: <s>$180.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$120.00</font></font>'];

document.getElementById('getquantity').onchange = function () {
var ind = document.getElementById('getquantity').selectedIndex;
document.getElementById('showprice').innerHTML = ' ' + textBlocks[ind];
}
}

第一个选择下拉菜单(使用 id="showprice"和 id="getquantity")

  <form id='product_qty' name="product_qty">
<img class="checkout" src="images/lean_green_usEN.jpg" usemap="#lean_green_usEN">
<map name="lean_green_usEN">
<area shape="rect" coords="168,140,341,189" class="addToCart">
</map>
<span class="showprice" id="showprice"></span>
<div class="qty"> Qty:
<select class="getquantity" id="getquantity">
<option value="" selected="selected">select quantity</option>
<option value="413">(1) box $35 ea</option>
<option value="414">(2) boxes $35 ea</option>
<option value="415">(4) boxes $30 ea</option>
</select>
</form>

第二个选择下拉菜单(使用 id="showprice2"和 id="getquantity2")

    <form id='burn_qty' name="burn_qty">
<img src="images/burn_control_usEN.jpg" usemap="#burn">
<map name="burn">
<area shape="rect" coords="168,140,341,189" class="addToCart">
</map>
<span class="showprice" id="showprice2"></span>
<div class="qty"> Qty:
<select class="getquantity" id="getquantity2">
<option value="" selected="selected">select quantity</option>
<option value="410">(1) box $35 ea</option>
<option value="405">(2) boxes $35 ea</option>
<option value="406">(4) boxes $30 ea</option>
</select>
</form>

第三个选择下拉菜单(使用 id="showprice3"和 id="getquantity3")

    <form id='energy_qty' name="energy_qty">
<img src="images/energy_mind_usEN.jpg" usemap="#energy">
<map name="energy">
<area shape="rect" coords="168,140,341,189" class="addToCart">
</map>
<span class="showprice" id="showprice3"></span>
<div class="qty"> Qty:
<select class="getquantity" id="getquantity3">
<option value="" selected="selected">select quantity</option>
<option value="409">(1) box $35 ea</option>
<option value="407">(2) boxes $35 ea</option>
<option value="408">(4) boxes $30 ea</option>
</select>
</form>

第 4 个选择下拉菜单(使用 id="showprice4"和 id="getquantity4")

    <form id='lean_qty' name="lean_qty">
<img src="images/lean_green_usEN.jpg" usemap="#lean">
<map name="lean">
<area shape="rect" coords="168,140,341,189" class="addToCart">
</map>
<span class="showprice" id="showprice4"></span>
<div class="qty"> Qty:
<select class="getquantity" id="getquantity4">
<option value="" selected="selected">select quantity</option>
<option value="413">(1) box $35 ea</option>
<option value="414">(2) boxes $35 ea</option>
<option value="415">(4) boxes $30 ea</option>
</select>
</form>

编辑

更新代码

在我的网站上,当我选择下拉菜单时弹出警报。它应该仅在未选择任何内容且单击按钮时弹出。

这是对我正在使用的 jquery 的两个引用。

<script src="//code.jquery.com/jquery-1.5.2.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.js"></script>

更新脚本<​​/b>

$(document).ready(function () {
$("form").on("click", ".addToCart", function (e) {
// e.delegateTarget is the form whose addToCart descendant was clicked
var quantVal = $(e.delegateTarget).find(".getquantity").val();

console.log(quantVal);

if (!quantVal) {
alert("Select Quantity!");
} else {
var productCode = quantVal;
window.location.href = "customer_appkit2.php?a=" + productCode + "&d=" + id + "&c=US&durl=" + idname;
}
});

var textBlocks = [
'<font class="font-color-grey">Retail Price: <s>$45.00</s></font><br>Sales Price: <font class="font-color-red">$35.00</font>',
'<font class="font-color-grey">Retail Price: <s>$45.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$35.00</font></font>',
'<font class="font-color-grey">Retail Price: <s>$90.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$70.00</font></font>',
'<font class="font-color-grey">Retail Price: <s>$180.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$120.00</font></font>'];

$('form').on('change', '.getquantity', function (e) {
var ind = this.selectedIndex;
$(e.delegateTarget).find(".showprice").html(textBlocks[ind]);
});
});

更新的选择下拉列表

<form id='burn_qty' name="burn_qty" autocomplete="off">
<div class="gadcontainer"> <img src="images/burn_control_usEN.jpg" width="350" height="225" border="0" usemap="#burn">
<map name="burn">
<area shape="rect" coords="168,140,341,189" class="addToCart">
</map>
<span class="showprice" id="showprice2"><font class="font-color-grey">Retail Price: <s>$45.00</s></font><br>
Sales Price: <font class="font-color-red">$35.00</font></span>
<div class="qty"> Qty:
<select class="getquantity" id="getquantity2">
<option value="" selected="selected">select quantity</option>
<option value="410">(1) box $35 ea</option>
<option value="405">(2) boxes $35 ea</option>
<option value="406">(4) boxes $30 ea</option>
</select>
</div>
</div>
</form>

编辑

上面的编辑代码现在可以工作了,因为我更新到了最新的 jQuery

<script src="//code.jquery.com/jquery-2.1.1-beta1.min.js"></script>

但是现在我的高光效果不工作了。

//-->

//<![CDATA[
$(window).load(function(){
$('.getquantity').change(function(){
$('.salesprice').effect("highlight", {}, 3000);
})
});//]]>

编辑

我按照@JLRishe 的建议在上面的脚本中加入了高亮效果。这样做可以使高光效果再次正常工作。我还将 jQuery 和 UI 更新为稳定版本。

这是更新后的脚本

cdn jQuery 和 UI

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>

包含高亮效果的更新脚本<​​/b>

$(document).ready(function () {
$("form").on("click", ".addToCart", function (e) {
// e.delegateTarget is the form whose addToCart descendant was clicked
var quantVal = $(e.delegateTarget).find(".getquantity").val();

console.log(quantVal);

if (!quantVal) {
alert("Select Quantity!");
} else {
var productCode = quantVal;
window.location.href = "url.com/2.php?a=" + productCode + "&d=" + id + "&c=US&durl=" + idname;
}
});

var textBlocks = [
'<font class="font-color-grey">Retail Price: <s>$45.00</s></font><br>Sales Price: <font class="font-color-red">$35.00</font>',
'<font class="font-color-grey">Retail Price: <s>$45.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$35.00</font></font>',
'<font class="font-color-grey">Retail Price: <s>$90.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$70.00</font></font>',
'<font class="font-color-grey">Retail Price: <s>$180.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$120.00</font></font>'];

$('form').on('change', '.getquantity', function (e) {
var ind = this.selectedIndex;
$(e.delegateTarget).find(".showprice").html(textBlocks[ind]);

$('.salesprice').effect("highlight", {}, 3000);

});
});

最佳答案

您应该能够执行以下操作:

$(document).ready(function () {
$("form").on("click", ".addToCart", function (e) {
// e.delegateTarget is the form whose addToCart descendant was clicked
var quantVal = $(e.delegateTarget).find(".getquantity").val();

console.log(quantVal);

if (!quantVal) {
alert("Select Quantity!");
} else {
var productCode = quantVal;
window.location.href = "url.com/2.php?a=" + productCode + "&d=" + id + "&c=US&durl=" + idname;
}
});

var textBlocks = [
'<font class="font-color-grey">Retail Price: <s>$45.00</s></font><br>Sales Price: <font class="font-color-red">$35.00</font>',
'<font class="font-color-grey">Retail Price: <s>$45.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$35.00</font></font>',
'<font class="font-color-grey">Retail Price: <s>$90.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$70.00</font></font>',
'<font class="font-color-grey">Retail Price: <s>$180.00</s></font><br><font class="salesprice">Sales Price: <font class="font-color-red-bold">$120.00</font></font>'];

$('form').on('change', '.getquantity', function (e) {
var ind = this.selectedIndex;
$(e.delegateTarget).find(".showprice").html(textBlocks[ind]);

$('.salesprice').effect("highlight", {}, 3000);
});
});

工作 fiddle (稍微修改以在没有图像的情况下在 JSFiddle 中工作):

http://jsfiddle.net/DDNLe/3/

我应该指出,您所有的表单都有未闭合的 div 元素,但我建议您关闭您的 imgarea 标签这在 HTML 中并不是绝对必要的。

关于javascript - 如何在具有不同 ID 的多个 Select 元素上使用相同的 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23102578/

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