我只想在选中复选框时显示textarea,并在未选中复选框时隐藏。但是当我选中某个复选框时,我当前的代码会显示所有textarea。 textarea和checkbox是动态生成的,因此可以有任意数量的checkbox和textarea。
基本上,我想做的是在复选框事件上切换textarea。
$( document ).ready(function() {
$(".with-us").hide();
$(".with-other").hide();
$('textarea[name="shopDescription"]').hide();
$("#work-option :radio").change(function () {
var workType = $('input[name=work]:checked').val();
if (workType == "wc") {
$(".with-other").hide();
$(".with-us").show();
} else if (workType == "woc") {
$(".with-us").hide();
$(".with-other").show();
} else {
$(".with-us").hide();
$(".with-other").hide();
}
});
$(document).on('click', 'input[name="shop"]', function () {
if (this.checked) {
$('textarea[name="shopDescription"]').show();
} else {
$('textarea[name="shopDescription"]').hide();
}
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="work-option">
<label>Show your work: </label>
<div class="options">
<input type="radio" name="work" value="wc">
With us
<input type="radio" name="work" value="woc">
With other
</div>
</div>
<div class="with-us">
<input type="hidden" name="withus" id="withus">
<input id="work1" type="checkbox" name="shop" value="test1">
Work 1
<textarea id="test1Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work2" type="checkbox" name="shop" value="test2">
Work 2
<textarea id="test2Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work3" type="checkbox" name="shop" value="test3">
Work 3
<textarea id="test3Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work4" type="checkbox" name="shop" value="test4">
Work 4
<textarea id="test4Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work5" type="checkbox" name="shop" value="test5">
Work 5
<textarea id="test5Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work6" type="checkbox" name="shop" value="test6">
Work 6
<textarea id="test6Description" placeholder="Description" name="shopDescription"></textarea>
</div>
<div class="with-other">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
&#13;
答案 0 :(得分:3)
选择器$('textarea[name="shopDescription"]').hide();
选择所有 textarea&#39; s
而是搜索next()
textarea:
$(document).ready(function() {
$(".with-us").hide();
$(".with-other").hide();
$('textarea[name="shopDescription"]').hide();
$("#work-option :radio").change(function() {
var workType = $('input[name=work]:checked').val();
if (workType == "wc") {
$(".with-other").hide();
$(".with-us").show();
} else if (workType == "woc") {
$(".with-us").hide();
$(".with-other").show();
} else {
$(".with-us").hide();
$(".with-other").hide();
}
});
$(document).on('click', 'input[name="shop"]', function() {
if (this.checked) {
$(this).next('textarea').show();
} else {
$(this).next('textarea').hide();
}
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="work-option">
<label>Show your work: </label>
<div class="options">
<input type="radio" name="work" value="wc"> With us
<input type="radio" name="work" value="woc"> With other
</div>
</div>
<div class="with-us">
<input type="hidden" name="withus" id="withus">
<input id="work1" type="checkbox" name="shop" value="test1"> Work 1
<textarea id="test1Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work2" type="checkbox" name="shop" value="test2"> Work 2
<textarea id="test2Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work3" type="checkbox" name="shop" value="test3"> Work 3
<textarea id="test3Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work4" type="checkbox" name="shop" value="test4"> Work 4
<textarea id="test4Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work5" type="checkbox" name="shop" value="test5"> Work 5
<textarea id="test5Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work6" type="checkbox" name="shop" value="test6"> Work 6
<textarea id="test6Description" placeholder="Description" name="shopDescription"></textarea>
</div>
<div class="with-other">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
&#13;
您可以稍微简化一下jQuery:
$(document).on('click', 'input[name="shop"]', function() {
$(this).next('textarea').toggle(this.checked);
});
答案 1 :(得分:1)
下面的代码将帮助您显示/隐藏文本框 此代码将在复选框
旁边显示/隐藏textarea if (this.checked) {
$(this).next("textarea").show();
} else {
$(this).next("textarea").hide();
}
$( document ).ready(function() {
$(".with-us").hide();
$(".with-other").hide();
$('textarea[name="shopDescription"]').hide();
$("#work-option :radio").change(function () {
var workType = $('input[name=work]:checked').val();
if (workType == "wc") {
$(".with-other").hide();
$(".with-us").show();
} else if (workType == "woc") {
$(".with-us").hide();
$(".with-other").show();
} else {
$(".with-us").hide();
$(".with-other").hide();
}
});
$(document).on('click', 'input[name="shop"]', function () {
if (this.checked) {
$(this).next("textarea").show();
} else {
$(this).next("textarea").hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="work-option">
<label>Show your work: </label>
<div class="options">
<input type="radio" name="work" value="wc">
With us
<input type="radio" name="work" value="woc">
With other
</div>
</div>
<div class="with-us">
<input type="hidden" name="withus" id="withus">
<input id="work1" type="checkbox" name="shop" value="test1">
Work 1
<textarea id="test1Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work2" type="checkbox" name="shop" value="test2">
Work 2
<textarea id="test2Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work3" type="checkbox" name="shop" value="test3">
Work 3
<textarea id="test3Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work4" type="checkbox" name="shop" value="test4">
Work 4
<textarea id="test4Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work5" type="checkbox" name="shop" value="test5">
Work 5
<textarea id="test5Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work6" type="checkbox" name="shop" value="test6">
Work 6
<textarea id="test6Description" placeholder="Description" name="shopDescription"></textarea>
</div>
<div class="with-other">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
您正在显示/隐藏所有textarea而不是该复选框旁边。要获取当前元素的下一个元素,您必须使用next()
方法立即获取下一个元素。
答案 2 :(得分:1)
CSS解决方案怎么样?
textarea {
display: none;
}
input[type="checkbox"]:checked +textarea {
display: block;
}
$( document ).ready(function() {
$(".with-us").hide();
$(".with-other").hide();
$("#work-option :radio").change(function () {
var workType = $('input[name=work]:checked').val();
if (workType == "wc") {
$(".with-other").hide();
$(".with-us").show();
} else if (workType == "woc") {
$(".with-us").hide();
$(".with-other").show();
} else {
$(".with-us").hide();
$(".with-other").hide();
}
});
});
&#13;
textarea {
display: none;
}
input[type="checkbox"]:checked +textarea {
display: block;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="work-option">
<label>Show your work: </label>
<div class="options">
<input type="radio" name="work" value="wc">
With us
<input type="radio" name="work" value="woc">
With other
</div>
</div>
<div class="with-us">
<input type="hidden" name="withus" id="withus">
<input id="work1" type="checkbox" name="shop" value="test1">
Work 1
<textarea id="test1Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work2" type="checkbox" name="shop" value="test2">
Work 2
<textarea id="test2Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work3" type="checkbox" name="shop" value="test3">
Work 3
<textarea id="test3Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work4" type="checkbox" name="shop" value="test4">
Work 4
<textarea id="test4Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work5" type="checkbox" name="shop" value="test5">
Work 5
<textarea id="test5Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work6" type="checkbox" name="shop" value="test6">
Work 6
<textarea id="test6Description" placeholder="Description" name="shopDescription"></textarea>
</div>
<div class="with-other">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
&#13;
答案 3 :(得分:1)
试试这个:
$( document ).ready(function() {
$(".with-us").hide();
$(".with-other").hide();
$('textarea[name="shopDescription"]').hide();
$("#work-option :radio").change(function () {
var workType = $('input[name=work]:checked').val();
if (workType == "wc") {
$(".with-other").hide();
$(".with-us").show();
} else if (workType == "woc") {
$(".with-us").hide();
$(".with-other").show();
} else {
$(".with-us").hide();
$(".with-other").hide();
}
});
$(document).on('click', 'input[name="shop"]', function () {
if($(this).is(':checked')) {
$(this).next().show();
} else {
$(this).next().hide();
}
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="work-option">
<label>Show your work: </label>
<div class="options">
<input type="radio" name="work" value="wc">
With us
<input type="radio" name="work" value="woc">
With other
</div>
</div>
<div class="with-us">
<input type="hidden" name="withus" id="withus">
<input id="work1" type="checkbox" name="shop" value="test1">
Work 1
<textarea id="test1Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work2" type="checkbox" name="shop" value="test2">
Work 2
<textarea id="test2Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work3" type="checkbox" name="shop" value="test3">
Work 3
<textarea id="test3Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work4" type="checkbox" name="shop" value="test4">
Work 4
<textarea id="test4Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work5" type="checkbox" name="shop" value="test5">
Work 5
<textarea id="test5Description" placeholder="Description" name="shopDescription"></textarea>
<input id="work6" type="checkbox" name="shop" value="test6">
Work 6
<textarea id="test6Description" placeholder="Description" name="shopDescription"></textarea>
</div>
<div class="with-other">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
&#13;