1

組み込みの確認ボックスと同じように、javascipt でカスタムメイドの確認ボックスを作成したいと考えています。組み込みの確認ボックスでは、ユーザーが少なくとも 1 つのことを選択しない限り、コードを進めることができません。以下は私のコードです: * ** * * HTML 開始* ** * *

<div class = "popUp confirm" style="z-index:40000;"  id="confirmBlock">         
<div id = "confirmLabel" >Confirm Message</div>
<div style ="border:0px solid red;height:44.56px;">
<input id="Confirm" type="button" value="Confirm" onclick = "confirmAction(1)" />
<input id = "CancelConfirm" type="button" value="Cancel" onclick = "confirmAction(0)" />
</div>
</div>

** * ** HTML終了* ** * *

** * ** Javascript の開始* ** * *

var confirmresult = "-1";

function confirmationLoop()
{
    alert("If this alert is preesnt it works, seems like the built in alert provides some sort of pause for other parts of code to continue to work");

    if(confirmresult == "-1")
        confirmationLoop();     

    return; 
} 

function confirmAction(val)
{


    confirmresult = val;    
}

function checkuuu()
{
    confirmresult = "1";
}

function confirmMessage(message)                
{   
    document.getElementById("confirmLabel").innerHTML= message;
    //var check = setTimeout(function(){confirmAction(1)},5000);
    confirmationLoop(); 
    /*
    while(1)            //using while almost does not allow any other part to run at all hence tried recursion
    {
        if(confirmresult != "-1")
            break;  
    }
    */
    document.getElementById("confirmLabel").innerHTML= "Confirm Message";   
    var returnVal = confirmresult;
    confirmresult = -1;
    return returnVal;
}

** * ** Javascript終了* ** * *

** * **サンプルコードの開始* ** * * だから、これは私が以下に期待するものです:

function example
{
    var check = confirmMessage(message);
    //the next part of code should not execute untill i press confirm or cancel, using settimeout or settimeinterval is asynchronous and the code flow continues. i want the effect something like alert and confirm built in boxes         
}

** * **サンプルコード終了* ** * *

ループを使用しましたが、スレッドが完全に占有されたままになり、ボタンを押す機会がありません。これは明らかでした。ただし、再帰により、他のアクティビティを自由に実行できます。確認ボタンを押すとconfirmResultの値が1になり、アラートで確認しても問題はありません。再帰ループ、つまり確認ループは 1 として読み取られないようです。それでも -1 として続きます。その確認ループにアラートを入れると、値は 1 として読み取られます。Ps=> 大変な質問で申し訳ありません!!!

4

1 に答える 1

1

どのような種類のループも使用できません。ブラウザがロックされるだけであることがわかっているためです。

必要なことは、「モーダル」ダイアログ ボックスをエミュレートすることです。

これは通常、重要なことに他のすべての要素をカバーする別の「オーバーレイ」要素の上にダイアログ ボックスを表示し、ユーザーによるそれらの要素との対話を防ぐことによって行われます。

またconfirm、値を返す関数を実装するのは非常に困難です。メソッドは同期的であるため、値を返すwindow.confirmことしかできません。ダイアログが表示されている間、他のすべての JS 処理をブロックします。

最も簡単な方法は、代わりに、ユーザーが目的の値を選択すると呼び出されるコールバック関数を提供することです。

于 2012-12-16T19:17:13.770 に答える