在javascript中创建是/否提示 [英] Creating a yes/no prompt in javascript

查看:63
本文介绍了在javascript中创建是/否提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络应用程序中与用户确认时我可以使用什么。



就像我们在windows应用程序中使用messagebox一样。



例如



如果我想删除数据表中的任何记录,那么它应该询问你确定要删除记录吗?并且提示应该说是和否,不是OK和取消。

What can I use during confirmation with the user in web application.

like we use messagebox in windows app.

For e.g

if i want to delete any record from data-table then it should ask "Are YOU Sure you Want to Delete Record?" and the prompt should say yes and no, not OK and Cancel.

推荐答案

你可以制作自定义Javascript警报和确认对话框,并更改对话框上的按钮文本。



这个reffer:

http://theinfiniteloopblog.com/programming/jscript/custom-javascript-alert-and-confirm -dialog-boxes /
u can make Custom Javascript Alert and Confirm Dialog boxes, and change the button text on dialog boxes.

for this reffer:
http://theinfiniteloopblog.com/programming/jscript/custom-javascript-alert-and-confirm-dialog-boxes/


你必须使用这样的叠加来伪造它。这个解决方案的操作方式与onclick =return ...方法的操作方式不同,但它适用于一些改编。

(你必须改进这个以使其成为可用的我我只是把这个空闲的手放在这里作为建议,所以它只是伪代码。

Def。用jQuery函数和访问器替换innerHTML和documentGetElement ......以确保它是跨浏览器兼容,并检查语法!!



CSS

You'd have to fake it using an overlay something like this. This solution won't operate the same way as the onclick="return... method, but it would work with some adaptation.
(you'll have to improve this yrself to make it useable I'm only tying this free hand here for the forum as a suggestion, so it's only pseudocode).
Def. replace the innerHTML and documentGetElement... with JQuery functions and accessors to make sure it's cross browser compatible, and check the syntax!!

CSS
<style>
	.confbox
	{
		visibility:hidden;
		width:300px;height:300px;
		z-index:999;
		position:absolute;
		display:block;
		top:30%;
		left:30%; //adjust the top and left values in the js to better position the box
	}
</style>





HTML



HTML

<div id="myConfirmBoxName" class="confbox"><h3></h3><p></p><span></span></div>
<a href="#" önclick="myConfirmbox.open();">delete this article</a>





Javascript:



Javascript:

var confirmBox = function(_id)
{
	this.id = _id;
	this.title = 'My dialog';
	this.text = 'Set this value to set the body text';
	this.options = new Array();
	this.domElement = document.getElementById(_id);
	this.visible = false;
	
	this.addOption = function(txt,val)
	{
		var o = [txt,val];
		options.push(o);
	}
	
	this.setContent = function(title,body)
	{
		this.domElement.getElementsByTagName('h3')[0].innerHTML = title;
		this.domElement.getElementsByTagName('p')[0].innerHTML = body;
		this.domElement.getElementsByTagName('span')[0].innerHTML = '';/delete/article/321/
		this.domElement.getElementsByTagName('h3')[0].innerHTML = title;
		this.domElement.getElementsByTagName('p')[0].innerHTML = body;
		this.domElement.getElementsByTagName('span')[0].innerHTML = '';
		for (var i=2; i<options.length();i++;)
		{
			txt = options[i][0]; //text to show
			val = options[i][1]; //action to perform. how you handle / process this depends how complex you want to get! callbacks etc... it's up to you.
			link = '<a href="'+val+'" önclick="document.getElementById(\'+this.id+\'">'+txt+'</a>';
			this.domElement.getElementsByTagName('span')[0].innerHTML += link;
		}
	}
	this.open = function(destination)
	{
		this.setContent();
		if (this.visible)
		{
			//Add a function here to UNlock the underlying webpage
			//Add a function here to center the box
			this.domElement.style.visibility = "visible";
			} else {
				//Add a function here to LOCK the underlying webpage
				this.domElement.style.visibility = "hidden";
			}
			visible = !visible;
		}
	}
}

/*and when you are ready to attach the script to the confirm box, ex. after DOM load event or similar, call this:*/

var myConfirmBox = new confirmBox('myConfirmBoxName');
myConfirmbox.title = 'the title i want to use';
myConfirmbox.text = 'the text body I want to show';
myConfirmbox.addOption('Yes',"www.codeproject.com"); 
myConfirmbox.addOption('No',"www.facebook.com");





使用javascript:yourfunction();在addOption val中调用函数而不是运行链接。如果你想让'true / false'返回到调用函数,你就会向confirmBox伪类添加一个回调处理程序,或者重写setContent()函数来处理以不同方式创建链接的方式。 ..



use javascript:yourfunction(); in the addOption val to call a function instead of running a link. if you want 'true/false' to be return to the calling function you cuold add a callback handler to the confirmBox pseudo-class, or re-write the setContent() function to handle the way the links are created in a different way...






用于在数据表上添加删除提示(看起来它将成为UI中的数据网格),您需要绑定javascript以在数据网格的ItemDataBound事件期间提示此警报。



因此,

事件,虽然数据是逐行绑定的 - 添加如下内容:

Hi,

For adding a delete prompt on a datatable (looks like its going to be a datagrid in UI), you would need to bind the javascript to prompt for this alert during the ItemDataBound event of the datagrid.

Thus,
in this event, while the data is binded row by row - add something like:
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1"); 
l.Attributes.Add("onclick", "javascript:return " +
    "confirm('Are you sure you want to delete this record')"); 

或者,

如果datagrid的ItemTemplate中的链接按钮暴露了OnclientClick事件(在ASP.NET2.0以后出现),你可以添加如下内容: />

Alternatively,
if the link button in ItemTemplate of datagrid has OnclientClick event exposed (which is present in ASP.NET2.0 onwards), you can add something like:

OnClientClick = 'return confirm("Are you sure you want to delete this entry?");' 



现在它的完成。



谢谢,

Sandeep。


Now its done.

Thanks,
Sandeep.


这篇关于在javascript中创建是/否提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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