gpt4 book ai didi

javascript - 如何基于值(value)展示

转载 作者:行者123 更新时间:2023-11-30 18:14:18 27 4
gpt4 key购买 nike

我正在尝试根据单选按钮的选择制作显示两个 div 之一的东西。就单选按钮而言,我有这段代码:

<input type="radio" name="radio-269" value="Purchase Only" />
<input type="radio" name="radio-269" value="Sale Only" />
<input type="radio" name="radio-269" value="Purchase and Sale" />

然后是我的DIV:

<div id="purchaseandsale" class="hidden" style="padding-top:10px; padding-bottom:10px; background-color: grey;">
Purchase and Sale
</div>

<div id="purchaseorsale" class="hidden" style="padding-top:10px; padding-bottom:10px; background-color: grey;">
Purchase or Sale
</div>

我的Javascript:

<script>
$("select[name='radio-269']").change(function() {
if ($(this).val() == "Purchase and Sale") {
$('#added_fields').removeClass("hidden");
}
else {
$('#added_fields').addClass("hidden");
}
});
</script>

首先,它不起作用,也没有在控制台中给我一个错误。如果显示“购买”或“销售”单选按钮,我怎样才能让它显示购买 div?

最佳答案

您应该引用 input[name='radio-269'] 而不是 input[select='radio-269']

此外,您的 ID 选择器与实际 DOM 的不匹配。

JavaScript 应该是:

$("input[name='radio-269']").change(function() {
if ($(this).val() == "Purchase and Sale") {
$('#purchaseandsale').removeClass("hidden");
}
else {
$('#purchaseandsale').addClass("hidden");
}
if ($(this).val() == "Purchase Only" || $(this).val() == "Sale Only") {
$('#purchaseorsale').removeClass("hidden");
}
else {
$('#purchaseorsale').addClass("hidden");
}
});

检查这个 JS Fiddle:http://jsfiddle.net/MrXenotype/ZpP7f/

关于javascript - 如何基于值(value)展示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13711416/

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