简体   繁体   English

jQuery.each未在函数中运行

[英]jQuery.each not running in function

I'm working on a project where some form elements depend on another form input to have a certain value, or possibly multiple elements with specific values before that input is shown. 我正在一个项目中,其中某些表单元素依赖于另一个表单输入来具有特定值,或者可能显示多个具有特定值的元素,然后再显示该输入。

The idea is that when the form is generated, the wrapping div for each input has a data-depends-on attribute with a comma-separated list of each field that it depends on to be shown, and the values for each that it's expecting to be shown. 想法是,在生成表单时,每个输入的包装div都有一个data-depends-on属性,该属性具有一个逗号分隔的列表,该列表取决于要显示的每个字段以及期望的每个字段的值被显示。

I almost have the front-end / JavaScript code down to do the lifting, but for some reason my jQuery.each() loop in a JavaScript function isn't running even though I've confirmed the array I'm trying to loop through a. 我几乎要降低前端/ JavaScript代码的工作量,但是由于某种原因,即使我已经确认要遍历的数组,我的JavaScript函数中的jQuery.each()循环也没有运行一种。 has content, and b. 有内容,并且b。 that the functioning is actually being called when it is expected to do so. 该功能实际上是在预期的时候被调用的。

First, I have the actual function call (which is called whenever a dependency input is changed): 首先,我有实际的函数调用(每当依赖项输入更改时都会调用):

checkShowField(keyed_depends, current_vals, targeted_element);

And then the function checkShowField() definition: 然后函数checkShowField()定义:

function checkShowField(keyed_dependencies, current_values, targeted_element)
{
    var hide_field = null;
    jQuery.each(keyed_dependencies, function(key, value)
    {
        if (value != current_values[key] && hide_field == null)
            hide_field = false;
    });

    if (hide_field == null)
        $(targeted_element).slideDown();
    else
        $(targeted_element).slideUp();
}

Also please note that the function call is placed in the proper place, and is actually being called. 另外请注意,函数调用放置在正确的位置,并且实际上已经被调用。 I just added the code on here to show everyone context of how the function is being called. 我刚刚在此处添加了代码,以向每个人展示如何调用该函数的上下文。 The function call is wrapped in $(document).ready(function() {...}. 函数调用包装在$(document).ready(function(){...}中。

So as you can see, in the function "checkShowField", I have a jQuery.each loop that should be looping through keyed_dependencies array, but in actuality, the loop isn't even running once. 如您所见,在函数“ checkShowField”中,我有一个jQuery.each循环,该循环应通过keyed_dependencies数组进行循环,但是实际上,该循环甚至没有运行一次。 Thoughts? 有什么想法吗?

It looks like the keyed_dependencies is not really what you think it is. 看起来keyed_dependencies并不是您真正想的那样。 Try adding debugger; 尝试添加debugger; statements before the .each line and maybe in the function as well. .each行之前的语句,也可能在函数中。 Then use inspector/debugger to review the data in the variables. 然后使用检查器/调试器检查变量中的数据。

You can check, if keyed_dependencies in argument list has a property length . 您可以检查参数列表中的keyed_dependencies是否具有属性length If so, jQuery assumes an array and might actually fail to run you loop. 如果是这样,jQuery假定一个数组,并且可能实际上无法运行您的循环。

If that is the case, try using vanilla JS: 如果是这种情况,请尝试使用香草JS:

for (var key in keyed_dependencies) {...}

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM