Jquery onChange 运行此函数
我创建了一个名为 billingOptions() 的函数,它与下拉(选择)元素关联。当用户在下拉列表中选择一个选项时,该函数就会运行。
下拉列表将通过页面动态添加...每次添加 billingOptions() 函数 onChange。我正在尝试使用 Jquery 从已更改的下拉列表中查找最接近的 class=rate 实例。请记住,下拉列表是动态添加的,因此可能有很多 .rate 实例...
我尝试使用“this”和“closest”来查找最近的类(我什至不确定您可以使用类具有最接近的函数)。下面是一些代码:
***我被要求提供 HTML 的准确结构(表的行是动态生成的。表是静态的)。
<table id="billTasks">
<tr> <input class="taskNameInput" type="text" size="52" placeholder="Task Name" name="task:1" disabled="disabled"> <select class="billOptions" onchange="billingOptions(this)">
<option class="fixedRate">Bill Fixed Rate</option>
<option class="hourly">Bill Hourly</option>
</select>
<input type="text" name="fixedRate" placeholder="Rate" class="fieldWidth100 rate"/>
</tr>
</table>
function billingOptions(){
$(this).closest('.rate').hide();
}
***编辑:此代码不起作用。目标是隐藏 input.rate 元素。
I have created a function called billingOptions() it is associated with a dropdown (select) element. When a user selects an option in the dropdown the function is run.
The dropdown will dynamically be added through the page...each time adding the billingOptions() function onChange. I am trying to use Jquery to find the closest instance of the class=rate from the dropdown that was changed. Keep in mind that the dropdowns are added dynamically so there could possible be many instances of .rate...
I have tried using "this" and "closest" to find the nearest class (i'm not even sure you can use a class with the closest function). Here is some code:
***I was asked to provide the exact structure of the HTML (the rows of the table are dynamically generated. The table is static).
<table id="billTasks">
<tr> <input class="taskNameInput" type="text" size="52" placeholder="Task Name" name="task:1" disabled="disabled"> <select class="billOptions" onchange="billingOptions(this)">
<option class="fixedRate">Bill Fixed Rate</option>
<option class="hourly">Bill Hourly</option>
</select>
<input type="text" name="fixedRate" placeholder="Rate" class="fieldWidth100 rate"/>
</tr>
</table>
function billingOptions(){
$(this).closest('.rate').hide();
}
***EDIT: this code does not work. The goal is to hide the input.rate element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:首先,您对
this
未传递给函数的担忧是对了一半。它正在被传递,但没有被使用。为您的函数提供一个命名参数,并引用该参数而不是this
:.rate
总是下一个元素吗?如果是这样:.rate
总是同级吗?是否只有一个.rate
同级?如果是这样:.rate
是否会与您的选择共享一个公共父级,并且是该公共父级中.rate
的唯一实例?如果是这样:如果这些都不适合您,请提供有关您的 HTML 结构的更多详细信息。
编辑:感谢您发布 HTML 结构。您的 HTML 中缺少
。假设您的真实代码包含
,并且假设每行只有一个
.rate
,请尝试以下操作:Edit: First off, you are half-right about your concern that
this
is not being passed to the function. It is being passed, but it is not being used. Give your function a named parameter, and reference that instead ofthis
:Is
.rate
always the next element? If so:Is
.rate
always a sibling? Will there only ever be one.rate
sibling? If so:Will
.rate
share a common parent with your select, and be the only instance of.rate
in that common parent? If so:If none of these works for you, please provide more detail on the structure of you HTML.
Edit: Thanks for posting the structure of your HTML. You are missing
<td>
in your HTML. Assuming your real code includes the<td>
, and assuming there is only one.rate
per row, try this:你还没有真正问过一个问题,除了与类最接近的工作之外,我设置了这个小提琴来向你展示它的作用:
http://jsfiddle.net/JAjFF/
如果您能告诉我们问题是什么,您是否尝试过上面的代码,它是否有效?如果没有的话是什么问题?那我就修改一下我的答案。
You haven't really asked a question, other than does closest work with a class, I set up this fiddle to show you that it does:
http://jsfiddle.net/JAjFF/
If you can tell us what is the question, have you tried the code above, does it work? What is the problem if not? Then I will amend my answer.
很难确切地知道您的代码想要做什么,但它存在一些问题。
首先,您将 DOM 元素传递到函数中,但您的函数没有声明参数,并且您没有访问传入的参数。此外,此上下文中的
this
指的是 Window 对象,而不是您想要的元素。将其更改为 this(传入命名参数并使用它代替this
)可以解决这个问题:您也可以使用
arguments
集合而不是使用命名参数,但我会建议上面的方法。下一个问题是
closest
方法会爬 DOM 树,直到找到与您传递给它的选择器相匹配的第一个元素。在您的情况下,由于this
引用 Window 对象,因此closest
不会找到与您的选择器匹配的祖先。上面显示的函数也不太可能解决该问题,因为从代码的外观来看,
.rate
不是所选元素的祖先。我真的需要更多关于 HTML 结构的信息,以帮助您选择正确的.rate
元素(例如,内部是否总是有一个.rate
元素)与select
相同的容器吗?如果是这种情况,这里有一个工作示例 )。It's difficult to know exactly what your code is trying to do, but there are a couple of problems with it.
Firstly, you are passing a DOM element into your function, but your function has no parameters declared, and you're not accessing the argument you pass in. Also,
this
in this context refers to the Window object, not the element you want it to. Changing it to this (passing in a named argument and using that in place ofthis
) would solve that:You could alternatively use the
arguments
collection instead of using a named argument, but I would suggest the above method over that.The next problem is that the
closest
method climbs the DOM tree until it finds the first element that matches the selector you pass it. In your case, asthis
refers to the Window object,closest
isn't going to find an ancestor matching your selector.It's also unlikely that the function shown above will solve that problem, as by the look of your code,
.rate
isn't an ancestor of the selected element. I would really need more information on how your HTML is structured to help you select the correct.rate
element (e.g. is there always going to be a single.rate
element, inside the same container as theselect
? If that is the case, here's a working example).Closest() 可以与 div 一起使用吗? 是,但它适用于祖先元素
如果您的 .rate 元素与 SELECT 元素位于同一级别,则应使用 .siblings()
如果您的 .rate 元素位于 SELECT 元素内部,则应使用.children()
如果您的 .rate 元素位于 SELECT 元素之外,则应使用 .parent()
如果您的 .rate 元素比 SELECT 元素高出几级以上,则应使用。 close() 来找到最接近的一个。
另外,你的功能很好,你只需要更新你的选择器。
Does closest() work with a div? YES, but it's for ancestor elements
If your .rate element is on the same level as your SELECT element, you should use .siblings()
If your .rate element is inside your SELECT element, you should use .children()
If your .rate element is right outside of your SELECT element, you should use .parent()
If your .rate element is more then a few levels above your SELECT element, you should then use. closest() to find the closest one.
Also, your function is fine, you just need to update your selector.