[英]Uncaught ReferenceError: function is not defined
我有一個“ a href”標簽,在單擊此標簽時,調用一個函數,但這不起作用,並且收到錯誤消息:
Uncaught ReferenceError: Gonder is not defined index.php:10
onclick
JavaScript代碼:
<script type="text/javascript">
$(document).ready(function(){
function Gonder(nereden, nereye) {
$.ajax({
type: "POST",
url: "/ara.php",
data: '{ "nereden":"' + nereden + '", "nereye":"' + nereye + '" }',
contentType: 'application/json; charset=utf-8',
success: function (result) {
$("#sonuc").html(result.d);
},
error: function (result) {
$("#sonuc").html(result.d);
}
});
}
});
</script>
html代碼:
<a href="javascript:void(0)" onclick="Gonder(100, 101);" title="">
click</a>
不起作用...
未定義功能-錯誤定義功能/包含錯誤可能會發生錯誤。
在您的情況下,至少這是錯誤的:
data: '{ "nereden":"' + nereden + '", "nereye":"' + nereye + '" }',
並且應該是:
data: { nereden: nereden, nereye: nereye },
正如我上面發表的評論,
我希望在准備好文檔之外定義功能會有所幫助並使用該功能。 因此,請按照如下所示准備好文檔之外的功能 ,並且我希望數據格式需要更改,因為您要求不發送到php代碼的數據使用Piwolli數據格式而不是您的格式。
function Gonder(nereden, nereye) {
$.ajax({
type: "POST",
url: "/ara.php",
data: '{ "nereden":"' + nereden + '", "nereye":"' + nereye + '" }',
contentType: 'application/json; charset=utf-8',
success: function (result) {
$("#sonuc").html(result.d);
},
error: function (result) {
$("#sonuc").html(result.d);
}
});
}
嘗試這個:
的HTML
<a id="test" href="javascript:void(0);" title="">click</a>
JS
$(document).ready(function(){
$("#test").on("click",function(e){
e.preventDefault();
Gonder(100,101);
});
function Gonder(nereden, nereye) {
$.ajax({
type: "POST",
url: "/ara.php",
data: '{ "nereden":"' + nereden + '", "nereye":"' + nereye + '" }',
contentType: 'application/json; charset=utf-8',
success: function (result) {
$("#sonuc").html(result.d);
},
error: function (result) {
$("#sonuc").html(result.d);
}
});
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.