2012-01-10 47 views
5

加載此代碼時出現問題。我相信它與noConflict有關,但我似乎無法做到。未定義Jquery函數

$(document).ready(
    function spouseName() { 
     if ($('#maritalstatus').val() == 'married') { 
     $('#spousename').attr("disabled", false); 
     } else { 
     $('#spousename').val(''); 
     $('#spousename').attr("disabled", true); 
     } 
    } 
); 

的方式,它適用於IE瀏覽器,但不是FF

+0

你得到了什麼問題? – KMan 2012-01-10 07:10:18

+0

有文本框,不應該被禁用,但它仍然如此,螢火蟲說spouseName未定義。 – Shakir 2012-01-10 07:21:08

+0

了問題與其他解決,使問題 功能spouseName { $(文件)。就緒( //休息了jQuery功能 的): } 感謝您的答案 – Shakir 2012-01-10 07:44:07

回答

3

感謝您的信息和答案,似乎這個線程幫助

Function not defined

firefox無法識別函數na當它在jquery document.ready函數中時。儘管它看起來非常規,但我所做的是將其包裹在裏面。我剛剛刪除了文檔,並且完美地工作。似乎鉻和FF不識別這裏的函數名稱?

function spouseName() { 
    if ($('#maritalstatus').val() == 'married') { 
    $('#spousename').attr("disabled", false); 
    } 
    else { 
    $('#spousename').val(''); 
    $('#spousename').attr("disabled", true); 
    } 

}

1

你的問題不是很清楚。你在用$.noConflict嗎?如果是這樣有文檔中的例子:

<script type="text/javascript" src="other_lib.js"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript"> 
    $.noConflict(); 
    jQuery(document).ready(function($) { 
     if ($('#maritalstatus').val() == 'married') { 
      $('#spousename').attr("disabled", false); 
     } 
     else { 
      $('#spousename').val(''); 
      $('#spousename').attr("disabled", true); 
     } 
    }); 

    // Code that uses other library's $ can follow here. 
</script> 

但在所有情況下,確保你已經正確地引用jQuery腳本本身嘗試使用它之前。

1

你忘了準備好後()

()的函數應該是:如果你還沒有包括jQuery的或進口你的JavaScript之下發生

$(document).ready(function() { 
    function spouseName() { // your code } 
    function anotherFunction() { } 
}); 
4

的 「未定義jQuery的」 錯誤。

它應該是這樣的:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> </script> 
<script type="text/javascript" src="myjsfile.js"> </script> 

myjsfile.js應該是這樣的:

$(document).ready(function(){ 
    every function inside this 
});