2

我的任务是prompt用一个自定义函数替换一个 Javascript 调用,该函数使用一个花哨的 Javascript 激活的模式对话框。

该函数被调用一次,并期望返回一个字符串,以及来自用户的输入。我无法控制函数的调用方式(它是一个外部库。)

制作模态并获得输入很容易。用户单击“提交/确定”按钮后,如何将输入返回给自定义提示函数的调用者?

jQuery 很好。

4

2 回答 2

3

浏览器实现的prompt()实现方式无法通过页面上运行的用户级 Javascript 进行复制。您必须使用回调函数。如果可能的话,您应该使用现成的解决方案。

我要说的是您的代码的用户不能拥有这个:

var result = customPrompt(...);

相反,他们必须有以下内容:

customPrompt({
    ...

    ok: function() {
        //when user clicked ok
    },

    cancel: function() {
        //when user clicked cancel
    }

});
//Code continues to run here and doesn't wait for the user to click ok or cancel
于 2013-08-02T19:35:54.617 回答
0

这就是您如何将用户的输入输入到与模态启动器相同的功能中,以便您可以处理它。

<input type="text" id="user-input" />
<a href="#" onClick="handlePrompt(false)">OK</a>

function handlePrompt(launchModal){
     if(launchModal){
         // Code to Launch Modal
     }else{
         var userInput = $("#user-input").val();
         // Do what you want with the input.
     }
}
于 2013-08-02T19:38:20.467 回答