引数を指定しているのに、関数が未定義を返すのはなぜですか

user9689724

これは私のコードが(完全に)どのように見えるかです

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が返されます。はい、私は関数を呼び出します(ご覧のとおり)。

sorabh86

私を信じてください、前の答えは正しい方法でしたが、あなたがあなた自身のフローを試したいなら、ここに解決策があります、それは遅くなります、多くの不要な変数、たくさんのコーディングなどあなたはからのコードで遊ぶことができます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]

編集
0

コメントを追加

0

関連記事

分類Dev

小道具を使用するときに関数の引数が未定義を返すのはなぜですか?

分類Dev

矢印関数onClickを使用しているのに、変数が未定義なのはなぜですか?

分類Dev

Javascript関数が未定義を返すのはなぜですか?

分類Dev

関数が未定義を返すのはなぜですか?

分類Dev

この必須関数の変数引数が未定義を返すのはなぜですか?

分類Dev

要素のcss関数に対してjQueryが未定義を返すのはなぜですか?

分類Dev

JavaScript関数呼び出しが未定義を返すのはなぜですか?

分類Dev

Javascript関数が作成時に「未定義」を返すのはなぜですか?

分類Dev

関数が常に未定義を返すのはなぜですか?

分類Dev

`return 1`が指定されているのにPython関数が1.0(float)を返すのはなぜですか?

分類Dev

関数がアイテムとして未定義の配列を返すのはなぜですか?

分類Dev

Python:異なる型の引数を型定義関数に渡してもエラーが発生しないのはなぜですか?

分類Dev

この再帰関数が未定義を返すのはなぜですか?

分類Dev

この非同期関数が未定義を返すのはなぜですか?

分類Dev

この関数が未定義を返すのはなぜですか?

分類Dev

このjavascript関数が未定義を返すのはなぜですか?

分類Dev

JavaScriptマップ関数が未定義を返すのはなぜですか?

分類Dev

console.log関数が未定義を返すのはなぜですか?

分類Dev

再帰関数が未定義を返すのはなぜですか?

分類Dev

JSマップ関数が未定義を返すのはなぜですか?

分類Dev

再帰関数が「未定義」を返すのはなぜですか?

分類Dev

.invoke()を最初から作成します。関数は機能するのにメソッドが未定義を返すのはなぜですか?

分類Dev

変数tabledataが未定義を返すのはなぜですか?

分類Dev

最初の数値として8を指定している場合でも、関数が正しい値を返すのはなぜですか?

分類Dev

.methodsを追加すると、変数が未定義として返されるのはなぜですか?

分類Dev

関数を `std :: function`に割り当てると、未定義の参照が発生するのはなぜですか?

分類Dev

関数が未定義を返すのはなぜですか?また、それをデバッグする方法は?

分類Dev

Ncurses-関数呼び出しに複数の引数が存在するのに、mvprintw()を使用して文字列を出力しないのはなぜですか?

分類Dev

単純なJavascriptOOPゲームで内部関数が未定義として返されるのはなぜですか

Related 関連記事

  1. 1

    小道具を使用するときに関数の引数が未定義を返すのはなぜですか?

  2. 2

    矢印関数onClickを使用しているのに、変数が未定義なのはなぜですか?

  3. 3

    Javascript関数が未定義を返すのはなぜですか?

  4. 4

    関数が未定義を返すのはなぜですか?

  5. 5

    この必須関数の変数引数が未定義を返すのはなぜですか?

  6. 6

    要素のcss関数に対してjQueryが未定義を返すのはなぜですか?

  7. 7

    JavaScript関数呼び出しが未定義を返すのはなぜですか?

  8. 8

    Javascript関数が作成時に「未定義」を返すのはなぜですか?

  9. 9

    関数が常に未定義を返すのはなぜですか?

  10. 10

    `return 1`が指定されているのにPython関数が1.0(float)を返すのはなぜですか?

  11. 11

    関数がアイテムとして未定義の配列を返すのはなぜですか?

  12. 12

    Python:異なる型の引数を型定義関数に渡してもエラーが発生しないのはなぜですか?

  13. 13

    この再帰関数が未定義を返すのはなぜですか?

  14. 14

    この非同期関数が未定義を返すのはなぜですか?

  15. 15

    この関数が未定義を返すのはなぜですか?

  16. 16

    このjavascript関数が未定義を返すのはなぜですか?

  17. 17

    JavaScriptマップ関数が未定義を返すのはなぜですか?

  18. 18

    console.log関数が未定義を返すのはなぜですか?

  19. 19

    再帰関数が未定義を返すのはなぜですか?

  20. 20

    JSマップ関数が未定義を返すのはなぜですか?

  21. 21

    再帰関数が「未定義」を返すのはなぜですか?

  22. 22

    .invoke()を最初から作成します。関数は機能するのにメソッドが未定義を返すのはなぜですか?

  23. 23

    変数tabledataが未定義を返すのはなぜですか?

  24. 24

    最初の数値として8を指定している場合でも、関数が正しい値を返すのはなぜですか?

  25. 25

    .methodsを追加すると、変数が未定義として返されるのはなぜですか?

  26. 26

    関数を `std :: function`に割り当てると、未定義の参照が発生するのはなぜですか?

  27. 27

    関数が未定義を返すのはなぜですか?また、それをデバッグする方法は?

  28. 28

    Ncurses-関数呼び出しに複数の引数が存在するのに、mvprintw()を使用して文字列を出力しないのはなぜですか?

  29. 29

    単純なJavascriptOOPゲームで内部関数が未定義として返されるのはなぜですか

ホットタグ

アーカイブ