gpt4 book ai didi

jquery - 如果选择单选按钮,则切换表单字段的可见性

转载 作者:行者123 更新时间:2023-12-01 06:46:58 25 4
gpt4 key购买 nike

我最初试图隐藏一组表单字段。然后,如果选择了单选按钮,则显示该组字段。然后,如果取消选择该 radio ,请再次隐藏它们。

前两个工作正常(最初隐藏字段,如果选择了 radio 则显示它们)。但当取消选择 radio 时,我无法让它们再次隐藏。我确信我遗漏了一些明显的东西,但我不知道它是什么。

http://jsfiddle.net/C5PMy/

$(document).ready(function() {

toggleFields(); //call this first so we start out with the correct visibility depending on the selected form values

//Hide the fields initially
$("#bsd-field-custom-105966-group").hide();
$("#bsd-field-custom-105967-group").hide();
$("#bsd-field-custom-105968-group").hide();
$("#bsd-field-custom-105978").hide();
$("#bsd-field-custom-105969-group").hide();
$("#bsd-field-custom-105970").hide();
$("#bsd-field-custom-105979").hide();
$("#bsd-field-custom-105971-group").hide();
$("#bsd-field-custom-105972").hide();


//Show the fields only if lodging is required
$("#custom-105965_0").change(function () {
toggleFields();
});


});
//this toggles the visibility of the other lodging fields depending on the current selected value of the "lodging required" field
function toggleFields() {
if ($(("#custom-105965_0").checked)) {

$("#bsd-field-custom-105966-group").show();
$("#bsd-field-custom-105967-group").show();
$("#bsd-field-custom-105968-group").show();
$("#bsd-field-custom-105978").show();
$("#bsd-field-custom-105969-group").show();
$("#bsd-field-custom-105970").show();
$("#bsd-field-custom-105979").show();
$("#bsd-field-custom-105971-group").show();
$("#bsd-field-custom-105972").show();
}

else {
$("#bsd-field-custom-105966-group").hide();
$("#bsd-field-custom-105967-group").hide();
$("#bsd-field-custom-105968-group").hide();
$("#bsd-field-custom-105978").hide();
$("#bsd-field-custom-105969-group").hide();
$("#bsd-field-custom-105970").hide();
$("#bsd-field-custom-105979").hide();
$("#bsd-field-custom-105971-group").hide();
$("#bsd-field-custom-105972").hide();
}
}

最佳答案

Fiddle Demo

改变

function toggleFields() {
if ($("#custom-105965_0").is(':checked')) {

$(".custom-105965").change(function () {//assign change handler to both radio buttons
toggleFields();
});

<小时/>更好、更短的代码版本

Fiddle Demo

$(document).ready(function () {
$("[id^=bsd-field-custom-1059]").hide();
$(".custom-105965").change(function () {
if (this.value == 'Yes') {
$("[id^=bsd-field-custom-1059]").show();
} else {
$("[id^=bsd-field-custom-1059]").hide();
}
});
});

<小时/>更短的版本

Fiddle Demo

$(document).ready(function () {
$("[id^=bsd-field-custom-1059]").hide();//hide all
$(".custom-105965").change(function () {//change event on radio button
$("[id^=bsd-field-custom-1059]").toggle(this.value == 'Yes'); //if selected radio button has value yes than return true i.e show else false i.e hide
});
});

Attribute Starts With Selector [name^="value"]

.toggle( showOrHide )

关于jquery - 如果选择单选按钮,则切换表单字段的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23692400/

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