【问题标题】:jQuery change function not workingjQuery更改功能不起作用
【发布时间】:2010-10-25 08:41:46
【问题描述】:

我似乎无法让这个工作看起来应该是这样的

$('.titleother').change(function() {

if ($('.titleother').val() == 'Other') {
    ​$('.hiddentext').css('display','block')
}​​​

})

对于这个 HTML

<select class="titleother">​
<option value="1">1</option>
<option value="2">2</option>
<option value="Other">Other</option>
</select>

<p class="hiddentext" style="display:none">TEXT</p>

有什么想法吗?

【问题讨论】:

  • 确定不是work?
  • 它似乎不适合我?
  • 代替 if ($('.titleother').val() == 'Other') ​$('.hiddentext').css('display','block')更好的方法是:$('.hiddentext').toggle($(this).val() == 'Other');

标签: jquery onchange


【解决方案1】:

这适用于我使用 Chrome:

$(function(ready){
    $('.titleother').change(function() {
        if ($(this).val() == 'Other') {
            $('.hiddentext').show();   
        }
    });
});

http://jsfiddle.net/amuec/11/

(Darin 的代码也不适合我)

【讨论】:

    【解决方案2】:

    确保将其放入 $(document).ready 中,以便在附加 .change() 处理程序时 DOM 准备就绪:

    $(function() {
        $('.titleother').change(function() {
            if ($(this).val() == 'Other') {
                ​$('.hiddentext').show();
            }​​​
        });
    });
    

    【讨论】:

    • 那个小提琴适合我吗?你确定你启用了javascript?
    • 是的,确实启用了 javascript - 我在 Mac 上使用 Safari - 但也在 Chrome 中尝试过,但它不起作用
    • @Jamie 你可以在一个单独的文件中尝试这个,而不是 jsfiddle 吗?这似乎是一个错误。
    • @DarinDimitrov,由于某种原因,您的代码有一个 \u200b 零宽度空间,导致代码无法运行。
    【解决方案3】:

    我认为你错过了;
    Check here

    【讨论】:

    • 你有什么错误吗?我在 Firefox 中测试过。它对我来说工作正常。
    • 您是否在寻找选择 1 和 2 时是否需要隐藏?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2015-08-09
    • 2018-01-27
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多