i have a "a href" tag and when on click this, calling a function but this not working and i receive an error:
我有一个“a href”标签,当点击这个时,调用一个函数,但这不起作用,我收到一个错误:
Uncaught ReferenceError: Gonder is not defined index.php:10
onclick
javascript code :
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 code :
HTML代码:
<a href="javascript:void(0)" onclick="Gonder(100, 101);" title="">
click</a>
not working...
3 个解决方案
#1
0
Function is not defined -error might happend when the function is defined incorrectly / has bugs in it.
函数未定义 - 当函数定义不正确/其中存在错误时,可能会发生错误。
In your case this at least is wrong:
在你的情况下,这至少是错误的:
data: '{ "nereden":"' + nereden + '", "nereye":"' + nereye + '" }',
And should be:
应该是:
data: { nereden: nereden, nereye: nereye },
#2
0
As comment posted above by me,
正如我上面发表的评论,
I hope defining the function outside the document ready will help and make use the function. so define that function out of document ready as like below, and i hope data format need to change as you asked the data not sending to php code use the Piwolli data format instead your's.
我希望定义文件外的功能将有助于使用该功能。所以如下所示定义该文件准备好的功能,我希望数据格式需要改变,因为你要求数据不发送到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);
}
});
}
#3
0
Try this:
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);
}
});
}
});
#1
0
Function is not defined -error might happend when the function is defined incorrectly / has bugs in it.
函数未定义 - 当函数定义不正确/其中存在错误时,可能会发生错误。
In your case this at least is wrong:
在你的情况下,这至少是错误的:
data: '{ "nereden":"' + nereden + '", "nereye":"' + nereye + '" }',
And should be:
应该是:
data: { nereden: nereden, nereye: nereye },
#2
0
As comment posted above by me,
正如我上面发表的评论,
I hope defining the function outside the document ready will help and make use the function. so define that function out of document ready as like below, and i hope data format need to change as you asked the data not sending to php code use the Piwolli data format instead your's.
我希望定义文件外的功能将有助于使用该功能。所以如下所示定义该文件准备好的功能,我希望数据格式需要改变,因为你要求数据不发送到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);
}
});
}
#3
0
Try this:
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);
}
});
}
});