为了确保问题更清晰,
我想在JAVASCRIPT中重新实现提示对象,因为我想同时从用户那里获得两个变量。
如果可以扩展,重新实现或覆盖此对象,请告诉我如何操作。如果没有,你有更好的解决方案吗?
感谢。
答案 0 :(得分:3)
您应该使用http://jqueryui.com/demos/dialog/#modal-form
之类的内容您需要将javascript分成对话框
所以,如果你有
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页面,目前会出错):
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)
答案 1 :(得分:2)
您可以使用分隔符将它们分开两个不同的值。
var result = prompt('enter two values seperated by a comma').split(',');
alert('first value: ' + result[0]);
alert('second value: ' + result[1]);
<强> DEMO 强>
答案 2 :(得分:1)
您可以在javascript中重新定义大多数内容,包括窗口提示,警报和确认方法,但最好定义方法并调用 it 而不是本机对象。例如,定义“提示器”,“警报器”或“确认器”方法。