Javascript jquery从另一个函数获取变量
我正在使用jquery raty rating插件,希望在单击时获取分数,并将其传递给第二个函数,该函数将通过ajax发送到php,并将其与反馈一起插入数据库。有人能帮我吗Javascript jquery从另一个函数获取变量,javascript,jquery,Javascript,Jquery,我正在使用jquery raty rating插件,希望在单击时获取分数,并将其传递给第二个函数,该函数将通过ajax发送到php,并将其与反馈一起插入数据库。有人能帮我吗 $(function() { $('.rate').raty({ click: function(score, evt) { var rate = score;//this is the variable I want to pass }, path : '../img',
$(function() {
$('.rate').raty({
click: function(score, evt) {
var rate = score;//this is the variable I want to pass
},
path : '../img',
size : 24,
half :true,
cancel : true,
cancelOff : 'cancel-off-big.png',
cancelOn : 'cancel-on-big.png',
starHalf : 'star-half-big.png',
starOff : 'star-off-big.png',
starOn : 'star-on-big.png',
starOn : 'star-on-big.png',
score: function() {
return $(this).attr('data-score');
}
});
//the below function is in included js file
$(function() {
$("#submit" ).click(function() {
//how can I access the above var here?
if(rate == "")
{
$(".error").html("Score is missing");
}
else{
$.ajax({
//send data
});
}
}));
}); 在变量前面加上
var
使其仅在该范围内可访问。这样做,您只能在.rate
的单击处理程序中使用它
而是要扩大范围,
// define here, in a scope both functions can access!
var rate = null;
$(function () {
$('.rate').raty({
click: function (score, evt) {
rate = score;
},
path: '../img',
size: 24,
half: true,
cancel: true,
cancelOff: 'cancel-off-big.png',
cancelOn: 'cancel-on-big.png',
starHalf: 'star-half-big.png',
starOff: 'star-off-big.png',
starOn: 'star-on-big.png',
starOn: 'star-on-big.png',
score: function () {
return $(this).attr('data-score');
}
});
//the below function is in included js file
$(function () {
$("#submit").click(function () {
if (rate == null) {
$(".error").html("Score is missing");
} else {
$.ajax({
//send data
});
}
});
});
您会注意到我已将您的
rate==”
更改为rate==null
;我已经查看了raty文档,对于click
处理程序,我能找到的唯一有效值是数字或null
(如果单击了“取消”)。因此,与null的比较将适用于未选择值的用户或清除值的用户(如果已启用该设置) 在变量前面加上var
使其仅在该范围内可访问。这样做,您只能在.rate
的单击处理程序中使用它
而是要扩大范围,
// define here, in a scope both functions can access!
var rate = null;
$(function () {
$('.rate').raty({
click: function (score, evt) {
rate = score;
},
path: '../img',
size: 24,
half: true,
cancel: true,
cancelOff: 'cancel-off-big.png',
cancelOn: 'cancel-on-big.png',
starHalf: 'star-half-big.png',
starOff: 'star-off-big.png',
starOn: 'star-on-big.png',
starOn: 'star-on-big.png',
score: function () {
return $(this).attr('data-score');
}
});
//the below function is in included js file
$(function () {
$("#submit").click(function () {
if (rate == null) {
$(".error").html("Score is missing");
} else {
$.ajax({
//send data
});
}
});
});
您会注意到我已将您的
rate==”
更改为rate==null
;我已经查看了raty文档,对于click
处理程序,我能找到的唯一有效值是数字或null
(如果单击了“取消”)。因此,与null的比较将适用于未选择值的用户或清除值的用户(如果已启用该设置) 在变量前面加上var
使其仅在该范围内可访问。这样做,您只能在.rate
的单击处理程序中使用它
而是要扩大范围,
// define here, in a scope both functions can access!
var rate = null;
$(function () {
$('.rate').raty({
click: function (score, evt) {
rate = score;
},
path: '../img',
size: 24,
half: true,
cancel: true,
cancelOff: 'cancel-off-big.png',
cancelOn: 'cancel-on-big.png',
starHalf: 'star-half-big.png',
starOff: 'star-off-big.png',
starOn: 'star-on-big.png',
starOn: 'star-on-big.png',
score: function () {
return $(this).attr('data-score');
}
});
//the below function is in included js file
$(function () {
$("#submit").click(function () {
if (rate == null) {
$(".error").html("Score is missing");
} else {
$.ajax({
//send data
});
}
});
});
您会注意到我已将您的
rate==”
更改为rate==null
;我已经查看了raty文档,对于click
处理程序,我能找到的唯一有效值是数字或null
(如果单击了“取消”)。因此,与null的比较将适用于未选择值的用户或清除值的用户(如果已启用该设置) 在变量前面加上var
使其仅在该范围内可访问。这样做,您只能在.rate
的单击处理程序中使用它
而是要扩大范围,
// define here, in a scope both functions can access!
var rate = null;
$(function () {
$('.rate').raty({
click: function (score, evt) {
rate = score;
},
path: '../img',
size: 24,
half: true,
cancel: true,
cancelOff: 'cancel-off-big.png',
cancelOn: 'cancel-on-big.png',
starHalf: 'star-half-big.png',
starOff: 'star-off-big.png',
starOn: 'star-on-big.png',
starOn: 'star-on-big.png',
score: function () {
return $(this).attr('data-score');
}
});
//the below function is in included js file
$(function () {
$("#submit").click(function () {
if (rate == null) {
$(".error").html("Score is missing");
} else {
$.ajax({
//send data
});
}
});
});
您会注意到我已将您的
rate==”
更改为rate==null
;我已经查看了raty文档,对于click
处理程序,我能找到的唯一有效值是数字或null
(如果单击了“取消”)。因此,与null的比较将适用于未选择值的用户或清除值的用户(如果已启用该设置) 可变速率在回调函数中定义。如果它是在函数内部定义的,则表示它是局部变量,它是有效的,并且只存在于该函数中。必须在此函数外定义此变量
如果您不能这样做-因为外部文件带有jQuery Raty代码,您无法编辑-因此在这种情况下-我认为唯一的可能性是读取DOM并获取jQuery Raty生成的隐藏输入值
但当值以编程方式更改时,输入不会触发更改事件,因此我不知道如何检测用户单击的评级图标……在回调函数中定义了可变利率。如果它是在函数内部定义的,则表示它是局部变量,它是有效的,并且只存在于该函数中。必须在此函数外定义此变量 如果您不能这样做-因为外部文件带有jQuery Raty代码,您无法编辑-因此在这种情况下-我认为唯一的可能性是读取DOM并获取jQuery Raty生成的隐藏输入值
但当值以编程方式更改时,输入不会触发更改事件,因此我不知道如何检测用户单击的评级图标……在回调函数中定义了可变利率。如果它是在函数内部定义的,则表示它是局部变量,它是有效的,并且只存在于该函数中。必须在此函数外定义此变量 如果您不能这样做-因为外部文件带有jQuery Raty代码,您无法编辑-因此在这种情况下-我认为唯一的可能性是读取DOM并获取jQuery Raty生成的隐藏输入值
但当值以编程方式更改时,输入不会触发更改事件,因此我不知道如何检测用户单击的评级图标……在回调函数中定义了可变利率。如果它是在函数内部定义的,则表示它是局部变量,它是有效的,并且只存在于该函数中。必须在此函数外定义此变量 如果您不能这样做-因为外部文件带有jQuery Raty代码,您无法编辑-因此在这种情况下-我认为唯一的可能性是读取DOM并获取jQuery Raty生成的隐藏输入值 但当值以编程方式更改时,输入不会触发更改事件,因此我不知道如何检测用户单击的评级图标