Jquery onChange 运行此函数

发布于 2024-11-28 09:05:19 字数 943 浏览 1 评论 0原文

我创建了一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仅冇旳回忆 2024-12-05 09:05:20

编辑:首先,您对 this 未传递给函数的担忧是对了一半。它正在被传递,但没有被使用。为您的函数提供一个命名参数,并引用该参数而不是 this

function billingOptions(el)
{
    $(el).something()...
}

.rate 总是下一个元素吗?如果是这样:

$(el).next(".rate").hide();

.rate 总是同级吗?是否只有一个 .rate 同级?如果是这样:

$(el).siblings(".rate").hide();

.rate 是否会与您的选择共享一个公共父级,并且是该公共父级中 .rate 的唯一实例?如果是这样:

$(el).closest(".commonParentClassName").find(".rate").hide();

如果这些都不适合您,请提供有关您的 HTML 结构的更多详细信息。

编辑:感谢您发布 HTML 结构。您的 HTML 中缺少 。假设您的真实代码包含 ,并且假设每行只有一个 .rate,请尝试以下操作:

function billingOptions(el)
{
    $(el).closest("tr").find(".rate").hide();
}

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 of this:

function billingOptions(el)
{
    $(el).something()...
}

Is .rate always the next element? If so:

$(el).next(".rate").hide();

Is .rate always a sibling? Will there only ever be one .rate sibling? If so:

$(el).siblings(".rate").hide();

Will .rate share a common parent with your select, and be the only instance of .rate in that common parent? If so:

$(el).closest(".commonParentClassName").find(".rate").hide();

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:

function billingOptions(el)
{
    $(el).closest("tr").find(".rate").hide();
}
念﹏祤嫣 2024-12-05 09:05:20

你还没有真正问过一个问题,除了与类最接近的工作之外,我设置了这个小提琴来向你展示它的作用:

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.

超可爱的懒熊 2024-12-05 09:05:20

很难确切地知道您的代码想要做什么,但它存在一些问题。

首先,您将 DOM 元素传递到函数中,但您的函数没有声明参数,并且您没有访问传入的参数。此外,此上下文中的 this 指的是 Window 对象,而不是您想要的元素。将其更改为 this(传入命名参数并使用它代替 this)可以解决这个问题:

function billingOptions(elem){
   $(elem).closest('.rate').hide();
}

您也可以使用 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 of this) would solve that:

function billingOptions(elem){
   $(elem).closest('.rate').hide();
}

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, as this 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 the select? If that is the case, here's a working example).

笑脸一如从前 2024-12-05 09:05:20

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文