【问题标题】:One jQuery Change Event on Multiple Elements多个元素上的一个 jQuery 更改事件
【发布时间】:2013-04-07 15:48:14
【问题描述】:

我有 3 个文本框,它们都具有相同的 id,我通过将其放入控制器数组中处理到 ASP 中

我有一个链接,可以在前 3 个下方添加无限数量的文本框。

我目前的变更声明:

    $('input.#invent').change(function () {

适用于第一个文本框上的更改事件, 但是具有相同信息的其他人在更改时不会触发它

当任何 3 个以上的文本框发生更改时,触发更改事件的最佳策略是什么??

【问题讨论】:

  • “我有 3 个文本框,都具有相同的 id” 那是你的问题。不要那样做。 id必须在文档中是唯一的。这是“标识符”的全部意义。
  • 你确定 `$('input.#invent')` 有效吗?这是无效的语法
  • 我是 jquery/javascript 的新手,但它确实有效。

标签: jquery html asp-classic


【解决方案1】:

最佳策略(95% 的时间):使用类为多个元素添加侦听器。 ID 应该是唯一的。类是为此而设计的,它将在未来为您提供最大的可扩展性。

HTML:

<input type="text" name="input1" class="invent" />
<input type="text" name="input2" class="invent" />

jQuery:

$('.invent').change(function () {
   // Do magical things
});

对于另外 5%:

如果您希望使用唯一 ID 或唯一字段名称而不是所选答案中描述的单个类,您可以为多个唯一命名的元素添加一个侦听器,如下所示:

HTML:

<input type="text" name="something1" id="invent1" />
<input type="text" name="something2" id="invent2" />
<input type="text" name="something3" id="invent3" />

你可以使用 jQuery multiple selectors

$('#invent1, #invent2, #invent3').change(function () {
   // Do magical things
});

OR 你可以使用 jQuery starts with 属性选择器:

//target based on what input id starts with
$('[id^="invent"]').change(function () {
   // Do magical things
});

// OR target based on input name attribute starts with
$('input[name^="something"]').change(function () {
   // Do magical things
});

【讨论】:

    【解决方案2】:

    将 ID 为 #invent 的所有三个元素都更改为一个类(ID 的 must to be unique),或者它只适用于第一个元素,就像您目前正在发生的情况一样。

    然后,您可以定位所有具有.invent 类的元素:

    $('input.invent').change(function () {
       // Here, $(this) refers to the specific element of the .invent class which 'changed'
    }):
    

    详细了解 ID 和 Class 选择器的区别here

    【讨论】:

    • 谢谢@lifetimes - 在我遇到这个解决方案之前,我正在向 SO 发布一个 Q。
    【解决方案3】:

    由于 id 是唯一的,因此您应该改用 class。然后您可以使用 each 遍历您的类并将$(this) 应用于目标当前change 输入:

    $('input.invent').each(function () {
        $(this).change(function () {
    
        });
    });
    

    【讨论】:

    • 当元素id 存在已知模式时,使用元素唯一的id 值可能有助于选择。如control_1235control_12543
    【解决方案4】:

    假设你的 HTML 是这样的:

    <input type="text" id="invent" />
    <input type="text" id="invent" />
    <input type="text" id="invent" />
    <input type="text" id="invent1" />
    <input type="text" id="invent2" />
    <input type="text" id="invent3" />
    

    现在 ID 必须是唯一的。因此,为所有输入添加一个类,例如 invent,HTML 将是:

    <input type="text" class="invent" />
    <input type="text" class="invent" />
    <input type="text" class="invent" />
    <input type="text" class="invent" />
    <input type="text" class="invent" />
    <input type="text" class="invent" />
    

    并像这样调用 on change 事件:

    // This would be called now for all the text-boxes
    $('input.invent').change(function () {
       // Your code here
    }):
    

    以防万一,您不能为所有文本框添加类。您只需这样做:

    $("input:text").change(function () {
       // Your code here
    }):
    

    【讨论】:

      【解决方案5】:

      @Eli 为您回答完全匹配的问题。如果您想阅读所有文本框,则可以使用以下方法。

        $('input[type=text]').each(function () {
                          $(this).change(function () {
                              alert($(this).val());
                              $(this).focus();
                          });
                      });
      

      【讨论】:

      • 为什么要这样做?看起来过于复杂,当你可以简单地做 $('input[type=text]').on('change', function(){....}); 甚至 $('input[type=text]').change(function(){....});
      【解决方案6】:

      您不能使用 ID 来引用多个元素。任何 HTML 页面上的 ID 都必须是唯一的!

      改用类:-)。

      【讨论】:

      • 没错。只是在多个元素上添加相同的 ID 在语义上也是错误的。记住 ID 可用于链接页面的特定部分,这是以前由锚标记完成的。不是您的答案,但请记住,在编写该标记时
      • 这在技术上是不正确的,您可以使用元素id 属性来捕获多个控件。关于模板,您可以使用已知的id 模式来捕获多个元素。
      猜你喜欢
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多