是否可以在JAVASCRIPT中扩展prompt()对象 [英] Is it possible to extend prompt() object in JAVASCRIPT

查看:181
本文介绍了是否可以在JAVASCRIPT中扩展prompt()对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了确保问题更清楚,

我想在JAVASCRIPT中重新实现提示对象,因为我想从用户那里获得两个变量时间。

I want to re-implement the prompt object in JAVASCRIPT because i want to get two variables from the user at the same time.

如果可以扩展,重新实现或覆盖此对象,请告诉我如何操作。如果没有,你有更好的解决方案吗?

If it is possible to extend, re-implement or override this object, please tell me how. If not, do you have better solutions?

谢谢。

推荐答案

您应该使用 http://jqueryui.com/demos/dialog/#modal-form

您需要将javascript分成对话框

You will need to split your javascript where you have your dialog

所以如果你有

function getAndUseUserInfo() {
   bla1();
   bla2();
   var x = prompt("Gimme something for bla 3","");
   if (x) bla3(x); // this will not be executed until user closes prompt
}

你现在需要

function getUserInfo() {
  bla1();
  bla2();
  var x = "";
  $( "#dialog-form" ).dialog({
    autoOpen: false,
    height: 300,
    width: 350,
    modal: true,
    buttons: { 
      "OK": function() { x = $("#someIdFromtheForm").val(); $(this).dialog("close");}
      "CANCEL": function() { $(this).dialog("close");}
    }
    close: function() {
      if (x) bla3(x);
    }
  });
}

或者如果你坚持要覆盖内置函数,你可以做类似的事情这个(由于我没有使用html页面,目前会出错):

Or if you insist to override the built-in function you can do something like this (which currently gives an error since I am not using an html page):

  var orgPrompt = window.prompt;
  var varone, vartwo;
  function saveVars(doc) {
    varone = doc.getElementById("x").value;
    vartwo = doc.getElementById("y").value
    return [varone,vartwo];
  }
  window.prompt=function(one,two) {
    var html = '<center><br><br>'+one+':<input type=text id=x><br>'+two+':<input type=text id=y><br><input type=button value=OK onclick=\'window.returnValue=window.dialogArguments.saveVars(document);window.close()\'/>';
    var res = showModalDialog('javascript:"'+html+'"',window,"dialogWidth:100px;dialogHeight:100px");
  }
  x = prompt('first name','last name')
  alert(x)

这篇关于是否可以在JAVASCRIPT中扩展prompt()对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆