2013-03-21 1 views
3

3 개의 양식 필드 중 1 개가 변경되면 업데이트되는 간단한 텍스트 블록을 만들려고합니다.jQuery <function>이 함수가 아닙니다.

은 여기 내 jQuery를하고 점점이 오류를 유지입니다 : TypeError: $(...).updateTitlePrefix is not a function

사람이 내가 잘못 여기서 뭘하는지 말해 줄래? 이것은 JSF 프로젝트는 그가 Richfaces 라이브러리에 포함되어 무엇 때문에 관련성,하지만 난 JQuery와 1.3.2에있어 경우

$ = jQuery.noConflict(); 

$(document).ready(function() { 

    $('#dataEntryForm\:studyId').updateTitlePrefix(); 
    $('#dataEntryForm\:formNumberQualifier').updateTitlePrefix(); 
    $('#dataEntryForm\:formVersionNumber').updateTitlePrefix(); 
}); 

// updates the titlePrefix when either the study#, form# or form version are changed  
$.fn.updateTitlePrefix() = function() { 
    $(this).change(function() { 
     $('#dataEntryForm\:titlePrefix').text($('#dataEntryForm\:formNumberQualifier').text() + $('#dataEntryForm\:formVersionNumber').text() + $('#studyId').text()) 
    }); 
} 

확실하지.

+0

문서가 이미 준비되어있을 때 코드를 실행하고있는 것처럼 보입니다. 그렇지 않으면 * "ReferenceError : 할당에서 왼쪽이 유효하지 않습니다."* 오류가 표시 될 수 있습니다. –

+0

나는 틀릴 수 있었다; 그러나 어느 쪽인지 나는 그것이'method' 인 것을 꽤 확신합니다 ... – faino

+0

그냥 호기심에서 벗어나서, 결국에는 선호 될 수 있습니다. 하지만 함수를 기본 함수 (바인딩 jq 함수가 아닌)로 남겨두고 그런 다음이를 의사 준비자의 선택기에 바인딩하는 것이 어떻습니까? 그렇게하면 나중에 함수를 재사용 할 수있는 유연성을 얻을 수 있습니다. 즉, 이벤트와 기능을 분리합니다. 지금은 단단히 결합되어 단일 목적으로 보인다. $ (function() { $ ('yourselectors'). 변경 (function() {당신의 기능 (this);}) }); – williambq

답변

14
$.fn.updateTitlePrefix = function() { 

승리하려면 괄호를 제거하십시오.

$.fn.updateTitlePrefix()은 함수 호출입니다. 방금 함수를 선언했기 때문에 함수를 호출하거나 호출 할 필요가 없습니다.

이것은 함수를 매개 변수로 기대하는 함수에서 일반적으로 나타나는 문제입니다. 예를 들어.

function requriesFn(myFunction) {} 
requiresFn(doStuff()); // incorrect (passes the return value of the function doStuff) 
requiresFn(doStuff); // correct (passes the function itself) 

$.ajax({ 
    error: $.noop() // incorrect 
    error: $.noop // correct 
}); 

// and my biggest pet peeve, 
$(document).ready(function() { 
    myFunction(); 
}); 
// simplify it 
$(document).ready(myFunction); 
// simplify it some more 
$(myFunction); 

기술적으로, 다른 함수를 리턴하는 함수를 호출 할 필요가있을 수 있지만, 일반적으로 그렇지 않다.

+0

+1 승리 : – Blowsie

+0

나는 jQuery 머신이 나에게 몇 초 만에 나를 곧게 할 줄 알았다. – Catfish

관련 문제