返回在jQuery中无法正常工作(Return not working correctly in jQuery)

编程入门 行业动态 更新时间:2024-10-25 13:19:00
返回在jQuery中无法正常工作(Return not working correctly in jQuery)

我有一堆ul ,每个ul包含一堆input 。 所讨论的函数从“搜索框”中获取一个字符串,并通过搜索所述ul每个input来尝试在每个ul找到它。 没有搜索到的字符串的每个input都是FLAGGED。 该函数计算FLAGGED input的数量,如果ul的FLAGGED input数等于ul中input的TOTAL数,则可以确定搜索到的字符串不存在于那个ul ,并说ul应该给一类hide-this 。

function findString(subject){ var increment = 0; //If string cannot be found, highlight input if ($(subject).val().indexOf("s") == -1){ $(subject).css("background-color", "#ff0000"); increment = 1; } console.log("Increment is " + increment + "!"); //Return value by which to increment error count (can be 1 or 0) return increment; } $(document).ready(function() { //When new term is entered into searchbox $('#query-input').bind('input', function(){ //For each UL in table $(".big-table ul:not(.big-table-headers)").each(function(i){ //Count total number of inputs in UL var inputCount = $(this).find("input").length; var errorCount = 0; console.log("input count is: " + inputCount); //Then for each input in UL, try to find string $(this).find("input").each(function(){ //Increment errorCount based on return value of findString() errorCount = errorCount + $(findString($(this))); }); console.log("errorCount is " + errorCount + "!"); /*If the number of inputs in the UL containing string is the same as the total number of inputs, then no inputs in the UL contain this string, and the entire ul should be hidden*/ if(errorCount == inputCount){ $(this).addClass("hide-this"); } }); }); });

findString()函数运行良好,并确定每个没有字符串的input ,但是当我尝试从该函数返回一个数值(1或0)到main函数时, findString()代替返回一个对象,我当然不能在任何数学运算中使用它。 我怀疑它与使用.each()迭代器有关,但我不确定。 有没有人有任何想法?

注意我强制该函数使用一串“s”进行测试,因此它实际上并不是从搜索框中直接输入,而是由搜索框调用该函数。

I've got a bunch of ul's, and each ul contains a bunch of input's. The function in question takes a string from a "search box" and tries to find it within each ul by searching each of the input's that are in said ul. Each input that DOESN'T have the searched string is FLAGGED. The function counts the number of FLAGGED input's, and if the number of FLAGGED input's in the ul is equal to the TOTAL number of input's in the ul, then it can be determined that the searched string does not exist in that ul at all, and said ul should be given a class of hide-this.

function findString(subject){ var increment = 0; //If string cannot be found, highlight input if ($(subject).val().indexOf("s") == -1){ $(subject).css("background-color", "#ff0000"); increment = 1; } console.log("Increment is " + increment + "!"); //Return value by which to increment error count (can be 1 or 0) return increment; } $(document).ready(function() { //When new term is entered into searchbox $('#query-input').bind('input', function(){ //For each UL in table $(".big-table ul:not(.big-table-headers)").each(function(i){ //Count total number of inputs in UL var inputCount = $(this).find("input").length; var errorCount = 0; console.log("input count is: " + inputCount); //Then for each input in UL, try to find string $(this).find("input").each(function(){ //Increment errorCount based on return value of findString() errorCount = errorCount + $(findString($(this))); }); console.log("errorCount is " + errorCount + "!"); /*If the number of inputs in the UL containing string is the same as the total number of inputs, then no inputs in the UL contain this string, and the entire ul should be hidden*/ if(errorCount == inputCount){ $(this).addClass("hide-this"); } }); }); });

The findString() function works well and does identify each input that doesn't have the string, but when I try to return a numeric value (1 or 0) from that function to the main function, findString() instead returns an object, which I of course cannot use in any mathematical operations. I suspect it has something to do with using .each() iterators, but I'm not sure. Does anyone have any ideas?

NOTE: I am forcing the function to use a string of "s" for test purposes, so it's not actually taking the direct input from the search box, but the function is being called by the search box.

最满意答案

不要将调用包装到jQuery arount findString 。 它应该是:

errorCount = errorCount + findString($(this));

您的代码正在尝试将jQuery对象添加到errorCount ,而不是findString返回的findString 。

Don't wrap a call to jQuery arount findString. It should be:

errorCount = errorCount + findString($(this));

Your code is trying to add a jQuery object to errorCount, instead of the number returned by findString.

更多推荐

本文发布于:2023-07-30 07:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336732.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   jQuery   correctly   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!