jquery - 选定的单选按钮显示带有 jQ​​uery 的文本输入

标签 jquery jquery-mobile

我有一组单选框,您可以在其中选择金额。最后一个选项是其他。我希望该选项在选中后显示金额字段。这是使用 jquery mobile 构建的。

jsFiddle link

<fieldset data-role="controlgroup">
<legend>Select Amount</legend>
<input type="radio" name="amount" id="50" value="50" checked="checked"/>
<label for="50">$50</label>

<input type="radio" name="amount" id="100" value="100"/>
<label for="100">$100</label>

<input type="radio" name="amount" id="250" value="250"/>
<label for="250">$250</label>

<input type="radio" name="amount" id="500" value="500"/>
<label for="500">$500</label>

<input type="radio" name="amount" id="1000" value="1000"/>
<label for="1000">$1000</label>

<input type="radio" name="amount" id="other" value="other"/>
<label for="other">Other</label>
    <div id="enter_amount">
        <label for="other_amount">Enter Amount:</label>
        <input type="text" name="other_amount" id="other_amount" value="" data-theme="d"  />
    </div>
</fieldset>

我需要写什么才能让它工作?我目前正在尝试以下操作:

$(document).ready(function(){
    $("#enter_amount").css("display","none");
        $("#other").click(function(){
        if ($('#other:checked')) {
            $("#enter_amount").slideDown("slow"); //Slide Down Effect
        } else {
            $("#enter_amount").slideUp("slow");  //Slide Up Effect
        }
    });
});

不幸的是,除了简单地隐藏加载时的输入框之外,这不起作用。

最佳答案

使用 .change 而不是 .click。我还修改了您的代码以考虑其他一些情况。 这是 fiddle

$(document).ready(function(){
    $('#enter_amount').hide(); 
    $('fieldset :radio').change(function(){
       if( this.id === 'other' )
           $('#enter_amount').slideDown('slow'); 
        else if( $('#enter_amount').is(':visible') )
           $('#enter_amount').slideUp('slow');               
    }); 
});

关于jquery - 选定的单选按钮显示带有 jQ​​uery 的文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7732809/

相关文章:

javascript - 3D div 转换重置

android - 关于使用 jQuery Mobile 和 html 开发 android 应用程序

jquery - 如何从 jquery 导航栏中删除第一个像素行?

jquery - iOS5 + jquery-mobile 过渡闪烁

Jquery Mobile - 添加动态选择选项

jquery - 使用 jquery 插件验证成功后如何在文本框旁边显示符号

javascript - 如何从jquery中的文本区域获取选定的文本?

javascript - 如何根据轴值更改 d3js 中的线条颜色?

javascript - 添加双击以 Draggable/droppable::javascript

css - jquery 移动 css 总是覆盖我的 css