これは私のコードが(完全に)どのように見えるかです
function getPassword() {
let pwd = ($('<input/>', {
//unrelated code
})).on('blur', function() {
//more unrelated code
} else {
$(this).css({
'color': 'gray',
'border': '1px solid green'
});
let cValue = $(this).val();
console.log(cValue);
let x = getPasswordConfirm(cValue);
}
});
return pwd;
}
function getPasswordConfirm(cValue) {
let pwd = ($('<input/>', {
//more unrelated code
})).on('blur', function() {
//unrelated code
} else {
if ($(this).val() !== cValue) {
console.log(cValue);
//this is where it goes wrong, cValue is undefined???
} else {
$(this).css({
'color': 'gray',
'border': '1px solid green'
});
console.log(cValue);
}
}
});
return pwd;
}
私が抱えている問題は次のとおりです。cValue
引数として使用しますが、関数に渡されません。console.logを実行cValue
すると、getPasswordConfirm()
関数でundefinedが返されます。関数を呼び出してパラメーターに引数を渡そうとする部分を拡大するには、次のようにします。
else {
$(this).css({
'color': 'gray',
'border': '1px solid green'
});
let cValue = $(this).val();
console.log(cValue); //this works
let x = getPasswordConfirm('haha');
}
});
これは、パラメーターcValueを使用して現在の値を評価する必要がある関数の部分です。
else {
if ($(this).val() !== cValue) {
console.log(cValue); //returns undefined
} else {
$(this).css({
'color': 'gray',
'border': '1px solid green'
});
console.log(cValue);
}
関数を追加/呼び出す方法:
$(getEblock('col-md-6 col-md-offset-3').append(getLoginName(), getUsrName(), getPassword(), getPasswordConfirm()).appendTo('form'));
しかし、引数として何を使用しても、getPasswordConfirm(cValue)
関数では常にundefinedが返されます。はい、私は関数を呼び出します(ご覧のとおり)。
私を信じてください、前の答えは正しい方法でしたが、あなたがあなた自身のフローを試したいなら、ここに解決策があります、それは遅くなります、多くの不要な変数、たくさんのコーディングなどあなたはからのコードで遊ぶことができますjsbin
// declare global
// getting form by id (jquery),
// it is always good practise to define your element in global scope
// we used $somename for jquery specific names
// remember $('somekelement') is a heavy function of jQuery
// which did lots of calculation behind the scene
// this is why i am declaring it only once
// it will help me to not used $('#myform') every time, need to access form
var $myform = $('#myform');
// for your logic i wanted two extra boolean to check if my password and confirm_password i created or not.
var isPassword = false; // by default not created
var isConfirmPassword = false; // false means not created true means created.
// this is your function that create a input and some logic as per your requirements
function getPassword() {
if(!isPassword) {
let pwd = $('<input/>', {
type:'text',
name:'password',
'placeholder':'Password'
}).on('blur', function() {
if(false) {
//more unrelated code
} else {
$(this).css({
'color': 'gray',
'border': '1px solid green'
});
let cValue = $(this).val();
console.log("Your cValue: "+ cValue); // here you are getting your values right.
let x = getPasswordConfirm(cValue); // here you are calling another function that generate another input
}
});
return pwd;
isPassword = true;
}
};
// if this function is generating new input, then it must not be called inside of getPassword function, because blur event may occur multiple times and each blur will create new confirm_password fields
// here i think logically not possible,
// only possible way here is to distribute your coding into two section
// 1st section will build all your dynamic input fields
// 2nd section will validate your inputs.
function getPasswordConfirm(cValue) {
window.cValue = cValue; // i saved cValue here in global namespace so next time.
console.log('argument cValue: '+cValue); // wola, you got the value here.
// but sadly, you didn't have c_pwd yet, so you couldn't be able to fire blur event here.
// Your blur event will be created but if will fire on out of focus, but next time cValue will not be passed
// because it will happened only one time or you will have multiple inputs each trying to catch a event before born to the world
if(!isConfirmPassword) {
let c_pwd = ($('<input/>', { // changed name to c_pwd
type:'text',
name:'confirm_password',
'placeholder':'Confirm Password'
})).on('blur', function() {
console.log('What was cValue: '+ window.cValue);
if(false) {
//unrelated code
} else {
if ($(this).val() !== cValue) {
console.log('====rock you got cValue here : '+cValue); // getting this value here as well. but problem is that you are getting multiple inputs
//this is where it goes wrong, cValue is undefined???
} else {
$(this).css({
'color': 'gray',
'border': '1px solid green'
});
console.log(cValue);
}
}
}.bind(this)); // you can also bind your subscope to their parent scope by bind funciton, in new ES6 we create arrow function (arg1, arg2) => {console.log(arg1,arg2);}
isConfirmPassword = true;
//return c_pwd; // comment this line and uncomment next line see result by yourself.
$myform.append(c_pwd); // i am appending confirm_password input on blur on password result will create multiple confirm boxes
}
};
$myform.append(getPassword());
// suppose your two function build your form then code for appending will be like that
//$myform.append(getPassword()).append(getPasswordConfirm());
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加