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. has content, and b. that the functioning is actually being called when it is expected to do so.
我几乎有前端/ JavaScript代码来完成提升,但由于某种原因我的JavaScript函数中的jQuery.each()循环没有运行,即使我已经确认我试图循环的数组一个。有内容,和b。当预期这样做时,实际上正在调用该函数。
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. Thoughts?
正如您所看到的,在函数“checkShowField”中,我有一个jQuery.each循环应该循环通过keyed_dependencies数组,但实际上,循环甚至没有运行一次。思考?
2 个解决方案
#1
0
You can check, if keyed_dependencies
in argument list has a property length
. If so, jQuery assumes an array and might actually fail to run you loop.
如果参数列表中的keyed_dependencies具有属性长度,则可以检查。如果是这样,jQuery假设一个数组,实际上可能无法运行循环。
If that is the case, try using vanilla JS:
如果是这种情况,请尝试使用vanilla JS:
for (var key in keyed_dependencies) {...}
Hope that helps.
希望有所帮助。
#2
0
It looks like the keyed_dependencies is not really what you think it is. Try adding debugger;
statements before the .each
line and maybe in the function as well. Then use inspector/debugger to review the data in the variables.
看起来keyed_dependencies并不是你认为的那样。尝试添加调试器;在.each行之前的语句,也可能在函数中。然后使用检查器/调试器查看变量中的数据。
#1
0
You can check, if keyed_dependencies
in argument list has a property length
. If so, jQuery assumes an array and might actually fail to run you loop.
如果参数列表中的keyed_dependencies具有属性长度,则可以检查。如果是这样,jQuery假设一个数组,实际上可能无法运行循环。
If that is the case, try using vanilla JS:
如果是这种情况,请尝试使用vanilla JS:
for (var key in keyed_dependencies) {...}
Hope that helps.
希望有所帮助。
#2
0
It looks like the keyed_dependencies is not really what you think it is. Try adding debugger;
statements before the .each
line and maybe in the function as well. Then use inspector/debugger to review the data in the variables.
看起来keyed_dependencies并不是你认为的那样。尝试添加调试器;在.each行之前的语句,也可能在函数中。然后使用检查器/调试器查看变量中的数据。