多个元素上的一个 jQuery 更改事件

2023-05-14前端开发问题
7

本文介绍了多个元素上的一个 jQuery 更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I have 3 textboxes, all with the same id's that I process into ASP by bringing it into a controller array

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

I have a link that adds an unlimited number of textboxes below the first 3.

我目前的变更声明:

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

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

works fine for the change event on the first textbox, but the others with the same information do not fire it when changed

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

What is the best strategy for getting the change event to fire when any of the 3+ textboxes change??

推荐答案

将 ID 为 #invent 的所有三个元素改为一个类(ID 的 必须是唯一的),否则它只适用于第一个元素,比如什么您的情况目前正在发生.

Change all three elements with the #invent ID to a class instead (ID's must to be unique), or it's only going to work for the first element, like what's currently happening in your case.

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

Then, you can target all of the elements that have the .invent class:

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

详细了解 ID 和类选择器之间的区别这里.

Read more about the difference between ID and Class selectors here.

这篇关于多个元素上的一个 jQuery 更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13