0

これは私のコードの一部です:

document.querySelector("#edit_<?= $row["item_id"]; ?>").addEventListener("submit", function(a){
    if(!completed){
        a.preventDefault();
    }else{
        return true;
    }

    if((function(){
        alertify.prompt("New Amount of Item : ", function(e, str){
            if(e){
                document.querySelector("#edit_<?= $row["item_id"]; ?> input[name=\"item_amount\"]").value = str;
                var completed = true;
                return true;
            }else{
                return false;
            }
        })
    )() == true){
        document.querySelector("#edit_<?= $row["item_id"]; ?>").submit();
    }
});

どこで<? $row["item_id"]; ?>何かを返します(無視してください、それは本当の問題ではありません)

alertify.prompt()しかし、問題は、取得するまで待ってからstr配置する必要があることです

document.querySelector("#edit_<?= $row["item_id"]; ?> input[name=\"item_amount\"")

(汚いコードですみません)

これを解決するにはどうすればよいですか?

PSは、すでに与えられているため、可能であればjQueryソリューションを使用しないことをお勧めします。Javascript のみのソリューションを使用してください。

4

4 に答える 4

0

プロンプト関数は、テキストの受信後に実行されるコールバック関数を受け取ります。そこでロジックを続けることができます

alertify.prompt("Message", function (e, str) { // str is the input text if (e) { // user clicked "ok" } else { // user clicked "cancel" } }, "Default Value");

注: Alertify の例から引用

于 2014-07-24T09:05:45.067 に答える
0

javascript(client) と php(server) を直接混ぜないでください。クライアントからサーバーへ、またはサーバーからクライアントへデータを渡すには、ajax を使用できます。

于 2014-07-24T09:06:08.187 に答える
0

if2 番目の条件を削除し、alertifyコールバック関数を使用します。

// define the completed variable outside the callbacks
var completed = false;
document.querySelector("#edit_<?= $row["item_id"]; ?>").addEventListener("submit", function(a){
    if(!completed){
        a.preventDefault();
    }else{
        return true;
    }
    alertify.prompt("New Amount of Item : ", function(e, str){
        if(e){
            document.querySelector("#edit_<?= $row["item_id"]; ?> input[name=\"item_amount\"]").value = str;
            //no var before completed, because then it would be local to this callback function
            completed = true;
            // here instead returning "true" do what you wanted to do if true
            document.querySelector("#edit_<?= $row["item_id"]; ?>").submit();
        }
    });
});
于 2014-07-24T09:11:40.517 に答える