0

나는 다음과 같은 기능을 갖고 있다고 value 현재 콘솔에 출력 중입니다.JQuery와 함수에서 값을 반환

(예 ..) 미리

function exampleCase() { 
    checkPanes(); //evidently, does not return anything. 
    //Ideally, I want the numerical value, being output to console in above function. 
} 

감사합니다 모든 제안/의견은 높이 평가됩니다.
환호

+0

'return activePane;'시도해 보셨습니까? – Sampson

답변

2

return activePane을 말한다 모두 잊어 버려. 작동하지 않습니다.

선택기를 재구성하는 것이 좋습니다. 사용할 선택자는 $("#slider .box .panel:visible")입니다. 이렇게하면 각 루프가 완전히 끊어집니다. 다음과 같은 예를 들어 당신은 코드를 재구성 수 :

function checkPanes() { 
    return $("#slider .box .panel:visible").index(); 
} 

function exampleCase() { 
    var visiblePane = checkPanes(); 

    // ... do something with the index 
} 

난 그냥 선택기를 사용하여 인라인보다는 새로운 기능을 제안 싶지만, 그건 당신이 같은 선택해야 특히, 취향의 문제이다 여러 곳에서.

function checkPanes() { 
    activePane = ''; 
    var panels = $("#slider .box .panel"); 
    //create a return array 
    visiblePanels = []; 
    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
    activePane = $(this).index()+1; 
    //add the result to the returnb array 
    visiblePanels[] = activePane 
    } 
    }); 
    // return results 
    return visiblePanels; 
} 

function exampleCase() { 
    var thepane = checkPanes(); 
    //now it has all the visible panels that were founded in the other function 
    // you can access them with thepane[0] or iterate on them 

} 

나는 이것이 당신이 필요하다고 생각 :

+0

루프에있을 수도 있지만, 표시가 보이는 상태에서/one/pane을 찾았 으면 루프에서 빠져 나갈 수 있습니다. – n00dle

+0

그에 따라 수정되었습니다. – lsuarez

+0

이것은 내가 성취하고자하는 것을 단순화 한 버전입니다. 감사합니다! –

4

그냥 루프에 주목했습니다. 반환 할 수있는 것처럼 보이는 것은 모든 활성 패널의 배열입니다 (이론적으로 둘 이상있을 수 있기 때문에).

function checkPanes() { 
    activePanes = []; 
    var panels = $("#slider .box .panel"); 

    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
    activePane.push($(this).index()+1); 
    console.log(activePane); 
    } 

    }); 
    return activePanes; 
} 

당신이 오직 하나의 활성화, 당신이 당신의 원래 방식으로 돌아가 바로 console.logreturn activePane을 추가 할 수있을 것입니다 알고 있다면.

+0

나는 그렇다고 생각한다. (나는이 모든 것에 대해 상당히 새로운 것이다.) - 다음 질문은 콘솔에 어떻게 기록 할까? 그 함수의 '반환'가치인가? 다른 곳에서 해당 반환 값을 사용 하시겠습니까? (아마도 반환 값을 기반으로 새 변수를 만들 수 있습니다) - 건배! –

+0

다른 함수에서'var activePanes = checkPanes(); '과 같은 것을하면, 지역 변수에 반환 값을 넣고 원하는대로 사용할 수 있습니다. –

0

그냥 return 문에 콘솔 라인을 전환 :

function checkPanes() { 
    activePane = ''; 
    var panels = $("#slider .box .panel"); 

    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
     activePane = $(this).index()+1; 
     return activePane; // Return the value and leave the function 
    } 

    }); 
} //END checkPanes(); 

전화하기 :

function exampleCase() { 
    var thepane = checkPanes(); //evidently, does not return anything. 
    // ... 
} 
0

을 나는 그것을 쉽게 생각 return activePane;

0

이 사용하는 것과 :

function checkPanes() { 
    activePane = ''; 
    var panels = $("#slider .box .panel"); 

    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
    activePane = $(this).index()+1; 
    } 

    }); 
    return activePane; 
} //END checkPanes(); 

이 : 그들은 그것이 jQuery를 each 루프에있어 보지 않았기 때문에

function exampleCase() { 
    var myval=checkPanes(); //evidently, does not return anything. 
    console.log(myval); 

}

+0

아아아, 그렇습니다. 콘솔에 로깅 할 값을 검색하기 전에 변수에 함수를 전달해야합니다. 감사합니다! –

+0

항상 필요한 것은 아닙니다. 그러나 대부분의 다른 답변에는 초기 함수가 초기에 반환되어 매우 나쁠 수 있습니다 (이 경우 거의 확실하지 않지만 현재 또는 미래에있을 수 있음). 내가 가지고있는 것은 왜 내가 변수를 사용하는지에 대한 다른 것들보다 "더 안전"하다. – ADW

0

당신은 두 번째 내부를 조작하는 첫 번째 함수 내부에 뭔가를 반환해야합니다.

0

당신은 당신의 코드를 유지하고 당신이

function checkPanes() { 
activePane = ''; 
var panels = $("#slider .box .panel"); 

    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
    activePane = $(this).index()+1; 
    console.log(activePane); //Logs to console. 
    return activePane; //Returns value also. 
} 

}); 
} 

그래서 여기에서 반환 된 값을 사용할 수 있습니다 또는 그냥 콘솔에 로그인 한 다른 곳의 값을 사용하려는 경우 단지 수익을 추가 할 수 있습니다.또는 텍스트로 어딘가에 disaply하려는 경우 문자열로 변환 - 내가 귀하의 질문에

function exampleCase() { 
    checkPanes(); //Now it will still write in console. but you dont need to use the return 

    alert(checkpanes()); //Would write it to console and to an alert! 
} 

을 이해하지만 문자열을 반환 할 수 있도록 보장합니다 이잖아.

관련 문제