[英]Uncaught ReferenceError: checkin is not defined at HTMLButtonElement.onclick
I am facing a problem with js function on button click. 我在按钮点击时遇到js功能问题。
<button class="checkin_button btn-block" onclick="checkin(345)"><i class="fa fa-map-marker"></i> Check In</button>
My jQuery function is: 我的jQuery函数是:
var CheckIn;
$(document).ready(function() {
CheckIn = function checkin(id) {
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {
action: 'foody_checkin',
'restaurant' = id,
},
beforeSend: function() {},
success: function() {}
});
}
});
But its not working. 但它不起作用。
checkin(345)
is a function, so do like below:- checkin(345)
是一个函数,所以如下所示: -
function checkin(id){
var ajaxurl = "abc.com" //an example that you have to define ajaxurl before using it
jQuery.ajax({
type : 'POST',
url : ajaxurl, // now you can use ajaxurl happily
data : {
action : 'foody_checkin',
'restaurant': id, // : needed instead of =
},
beforeSend : function(){},
success : function(){}
});
}
Notes:- 笔记:-
1.Please take care of comments written inside quote. 1.请注意报价内写的评论。
2.Also check that jQuery library added before you script code 2.在脚本代码之前还要检查是否添加了jQuery库
Try to using data-id method html 尝试使用data-id方法html
<button class="checkin_button btn-block" data-id='345'><i class="fa fa-map-marker"></i> Check In</button>
jquery jQuery的
$(document).on('click', '.checkin_button', function () {
var id = $(this).data('id');
console.log(id);
// write your ajax below
});
Here is the working jsfiddle: https://jsfiddle.net/nuxtw2ft/ 这是工作的jsfiddle: https ://jsfiddle.net/nuxtw2ft/
You need to use this keyword to assign value to the var declared. 您需要使用此关键字为声明的var赋值。 Call the same on click of button. 点击按钮调用相同的内容。
Also assign value to ajaxurl as AlivetoDie stated in his answer. 同时为AlivetoDie在答案中说明的ajaxurl赋值。
You can run the code snippet below and verify. 您可以运行下面的代码段并验证。
var CheckIn; $(document).ready(function() { var ajaxurl="yourservice.com" this.CheckIn = function checkin(id) { console.log("CheckIn function called on click"); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'foody_checkin', 'restaurant': id, }, beforeSend: function() {}, success: function() {} }); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="checkin_button btn-block" onclick="CheckIn(345)"><i class="fa fa-map-marker"></i> Check In</button>
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.