我创建了一个单选
按钮用于选择,并且当用户单击时有“其他”按钮,它将打开文本框
以便用户可以手动指定里面有一些文字。
<label><input type="radio" name="option" value="">Web</label>
<label><input type="radio" name="option" value="">Mobile Apps</label>
<label><input type="radio" name="option" value="other">Other</label>
<input typt="text" placeholder="Specify here" id="other-text">
和 JavaScript
$(".option").change(function () {
//check if the selected option is others
if (this.value === "other") {
//toggle textbox visibility
$("#other-text").toggle();
}
});
这是我尝试过的方法,但不知何故它不起作用。
我是 javascript
的新手,所以请帮助我。
提前致谢。
如果可能的话你能告诉我如何放置jsfiddle/codepen
链接吗?
最佳答案
请尝试这个:
$(".js-option").click(function () {
//check if the selected option is others
if ($(this).val() == "other") {
$("#other-text").show();
}else{
$("#other-text").hide();
}
});
#other-text{
display:none;
}
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
<label><input type="radio" name="option" class="js-option" value="">Web</label>
<label><input type="radio" name="option" class="js-option" value="">Mobile Apps</label>
<label><input type="radio" name="option" class="js-option" value="other">Other</label>
<input typt="text" placeholder="Specify here" id="other-text">
关于单选按钮其他选项的 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38496851/