简体   繁体   English

单选按钮的Javascript功能其他选项

[英]Javascript Function for radio button other option

I've created a radio buttons for selection and there is "other" button when clicked by user it'll open text-box so that user can manually specify some text inside it. 我创建了一个radio按钮供选择,当用户单击时还有一个“其他”按钮,它将打开text-box以便用户可以在其中手动指定一些文本。

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

And javascript 和JavaScript

$(".option").change(function () {
//check if the selected option is others
if (this.value === "other") {
    //toggle textbox visibility
    $("#other-text").toggle();
}
});

This is what i've tried but somehow it's not working. 这是我尝试过的方法,但是某种程度上它不起作用。 I'm newbie to javascript so please help me. 我是javascript新手,请帮帮我。

Thanks in advance. 提前致谢。

If possible can you also tell me how to put jsfiddle/codepen links? 如果可能,您还能告诉我如何放置jsfiddle/codepen链接吗?

Please try this: 请尝试以下方法:

 $(".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"> 

Try this 尝试这个

JAVASCRIPT JAVASCRIPT

$("[name='option']").change(function () {
    //check if the selected option is others
    if (this.value === "other") {
        //toggle textbox visibility
        $("#other-text").show();
    } else {
        //toggle textbox visibility
        $("#other-text").hide();
    }
});

You are using a class selector $(".option") . 您正在使用类选择器$(".option") But you have not assigned any class to it. 但是您尚未为其分配任何类。 Unless you assign a class you have to use jquery input name selector to target the element 除非您分配一个类,否则必须使用jquery输入名称选择器来定位该元素

$( "input[name='option']" ).change(function(){
// $(".option").change(function () {  // if using class
if (this.value === "other") {
    $("#other-text").show();
}
else{
 $("#other-text").hide();
}
});

Check this JSFIDDLE 检查这个JSFIDDLE

Set other text box display none first ! 设置其他文本框不显示任何内容! Then display text when click radio button other ... 然后单击other单选按钮时显示文本...

<input typt="text" placeholder="Specify here" id="other-text" style="display:none">

<script type="text/javascript">
    $(":radio").change(function () {
        //check if the selected option is others
        $("#other-text").hide();
        if (this.value === "other") { 
              $("#other-text").show();
        }
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM