-2
我有一些變量,我想在jQuery的焦點事件中使用。我如何聲明它們以便它們處於焦點回調中?Javascript/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或空字符串
閱讀本文 - http://stackoverflow.com/questions/500431/javascript-variable-scope –
首先,你在調用'focus'時沒有關閉''''。 –
對不起,這不是語法 –