【问题标题】:jQuery event when particular option is selected选择特定选项时的 jQuery 事件
【发布时间】:2013-06-06 12:10:31
【问题描述】:

选择选择的特定选项时,我想将代码添加到DIV中。

这是代码,我不知道为什么不起作用。

<select id="selectbasic ref" name="selectbasic ix-select-how" class="input-xlarge">
    <option value="" label=""></option>
    <option value="SearchEngine" label="Suchmaschine">Suchmaschine</option>
    <option value="Webhostlist" label="Webhostlist">Webhostlist</option>
    <option value="Press" label="Presse">Presse</option>
    <option value="Friend" label="Bekannte">Bekannte</option>
    <option value="ByChance" label="Zufällig">Zufällig</option>
    <option value="Others" label="Sonstige">Sonstige</option>
</select>
<div class="control-group ix-hidden"></div>                    

还有 jQuery:

$('#ref').change(function () {
    if ($(this).val() === 'Others') {
        $("div.control-group.ix-hidden").append("<label class='control-label'>Wie?</label><div class='controls'><div class='input-prepend'><span class='add-on'><i class='icon-globe'></i></span><input type='text' class='input-xlarge' id='email' name='email' placeholder='Wie?'></div></div>");
    }
});

【问题讨论】:

  • 你给 2 id 的 selectbasic 和 ref,这还能用吗?

标签: jquery select append option


【解决方案1】:

为单个元素提供两个 ID 不是有效的 HTML,并且会导致尝试与 DOM 交互的任何 javascript 中出现未定义的行为。从选择中删除第二个ID,它工作正常:

<select id="ref" name="selectbasic ix-select-how" class="input-xlarge">

http://jsfiddle.net/ueEEW/

Why can't you have two IDs?

【讨论】:

    【解决方案2】:
    <select id="selectbasic ref" ...>
    

    属性ID必须是单一的:

    <select id="ref" ...>
    

    【讨论】:

    • 就是这样。谢谢!
    【解决方案3】:

    请检查您的 jQuery 选择器。我认为您的 id 应该没有空格(尝试id = 'selectbasicRef'),并且在您的 jQuery 代码中引用它时,它应该是 $("#selectbasicRef").change(function(){ //code });

    希望对你有帮助!

    【讨论】:

      【解决方案4】:

      第一件事是任何字段的属性ID都应该是单一的,并且ID属性名称之间没有空格。例如

      <select id="ref" name="selectbasic ix-select-how" class="input-xlarge">
      

      希望对你有帮助。

      【讨论】:

        【解决方案5】:

        无法使用空格分隔的列表将多个 id 分配给同一个 id 属性

        通过以下链接,它应该可以澄清您对多个 id 的疑虑

        Can an html element have multiple ids?

        【讨论】:

          猜你喜欢
          • 2019-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-30
          • 1970-01-01
          • 1970-01-01
          • 2012-09-26
          • 1970-01-01
          相关资源
          最近更新 更多