我有一些变量要在 jQuery 的焦点事件中使用。我如何声明它们以便它们在焦点回调中?
var height = 72;
$('#myfield').focus(function() {
heightinfeet = height / 12;
alert(heightinfeet);
});
对我来说,高度始终为 0。有几个变量要传递,我尝试将其用作属性的对象,但也没有传递它们
/ *真实代码* /
function GOPRO_pfFieldConfirmation(oField1, oField2, oMarkerField, bCaseSens) {
var GOPRO_iFieldLen1 = $(oField1).val().length;
var GOPRO_iFieldLen2 = $(oField2).val().length;
var GOPRO_sFieldVal1 = $(oField1).val();
var GOPRO_sFieldVal2 = $(oField2).val();
var GOPRO_sFieldSub = GOPRO_sFieldVal1.substring(0, GOPRO_iFieldLen2);
//if not case sensitive, make all upper case
if (bCaseSens == false) {
GOPRO_sFieldVal1 = GOPRO_sFieldVal1.toUpperCase();
GOPRO_sFieldVal2 = GOPRO_sFieldVal2.toUpperCase();
GOPRO_sFieldSub = GOPRO_sFieldSub.toUpperCase();
}
$(oField1).focus( function() {
console.log(GOPRO_iFieldLen1);
if ((GOPRO_sFieldVal1 == GOPRO_sFieldVal2) && GOPRO_iFieldLen1 > 0) {
GreenMarker();
}
else if (GOPRO_iFieldLen1 > 0 && GOPRO_iFieldLen2 > 0) {
RedMarker();
}
else {
NoMarker();
}
});
...
GOPRO_pfFieldConfirmation($('#reg_email'), $('#reg_email2'), $('.field_confirm:eq(3)'), false);
console.log 始终为 0,实际上所有预定义的变量都返回 0 或空字符串