单选按钮其他选项的 Javascript 函数

标签 javascript jquery html

我创建了一个单选按钮用于选择,并且当用户单击时有“其他”按钮,它将打开文本框以便用户可以手动指定里面有一些文字。

<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/

相关文章:

javascript - 根据滚动位置隐藏和显示导航箭头

javascript - 如何为 <noscript> 标签编写自动化测试?

javascript - 带有 css 的 jade 中的按钮不起作用

javascript - 在 selenium/appium 中,有一种方法可以在单击按钮等后检查/验证单击操作是否成功或不成功

javascript - "prepend"CSS 转换

javascript - jQuery 不隐藏 div (mvc 4)

javascript - Angularjs:在 TextArea 中打印带有新行的数组模型

javascript - Expressjs Mongoose 查找嵌套嵌入文档未定义

javascript - 如何以更好的性能替换字符串中的大量单词?

jQuery 窗口大小