javascript警报功能可在第二次点击时使用 [英] javascript alert function working on second click
问题描述
JavaScript警报功能可在第二次单击时使用..我正在将脚本放置在母版页上.在添加到购物车"按钮上,我正在调用该警报功能.但是在第一次单击添加到购物车"按钮时,它不起作用..但是在第二次单击时.弹出一个透明框,可以正常工作
我只是从gridview中找到按钮ID
JavaScript alert function works on second click.. i am putting script on master page. and on add to cart button i am calling that alert fuction. but on first click on add to cart button its doesn''t work.. but on second click. A clear box pops up and works fine
i am just finding the button id from gridview
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Button btn = (Button)GridView1.Rows[i].FindControl("btnaddcart");
//Change the property settings for the button in your TemplateField
btn.Attributes["onclick"] = "showstock()";
}
我正在调用位于gridview上的JavaScript函数.我只希望它在第一次单击时有效.但仍在进行第二次单击以添加到购物车按钮.
JavaScript代码是
and i am calling that JavaScript function which is on gridview. i just want that it works on first click. but ist working on second click to add to cart button.
the JavaScript code is
<script type="text/javascript" language="javascript">
function showstock()
{
alert("Sorry This Product is not in stock!");
}
</script>
推荐答案
您好,您在错误的位置调用了js函数,请在gridview的rowdatabound事件中编写以下代码.
Hi, You have called js function at wrong place, pls write below code in rowdatabound event of gridview.
btn.Attributes.Add("onclick","showstock();");
亲爱的朋友,
尝试将您的代码如下所示:-
Dear Friend,
Try to put your code like this:-
btn.Attributes.Add("onclick","showstock();");
我希望这能解决问题.
谢谢
I hope this will work out for solution.
Thanks
您在哪里尝试运行此循环以添加按钮.
之后,您是否正在gridview上执行数据绑定?
如果您在gridview.rowcreated事件上添加了这些调用Java脚本功能的按钮",它们应该可以工作.
尝试以下类似的方法(只是写了未测试的内容)
where are you trying to run this loop to add buttons.
are you doing a databind on gridview after that ?
If u add these ''buttons calling java-script function'' on gridview.rowcreated event, they should work.
try something like below (just wrote it not tested)
void GridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
Button btn= (Button)e.Row.FindControl("btnaddcart");
btn.Attributes["onclick"] = "showstock()";
}
}
这篇关于javascript警报功能可在第二次点击时使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!