[英]why my webpage is refreshing when i am calling a JavaScript function in my aspx page on a html image click event?
i am using a html image control in mt aspx page and i am calling a javascript function on the click event of that control but here my page is getting refreshed every time i am clicking it我在 mt aspx 页面中使用了一个 html 图像控件,我在该控件的点击事件上调用了一个 javascript 函数,但是每次我点击它时我的页面都会刷新
here's my code:这是我的代码:
<input id="wbsnew1" type="image" value="New" onclick="showHideDiv()" src="images/New.png" />
and here's my javascript code :这是我的 javascript 代码:
function showHideDiv() {
document.getElementById("newdata2").style.display = "block";
}
"newdata2" is a div “newdata2”是一个div
<input id="wbsnew1" type="image" value="New" onclick="return showHideDiv();" src="images/New.png" />
function showHideDiv() {
document.getElementById("newdata2").style.display = "block";
return false;
}
Try to return after applying the function like the following code :尝试在应用函数后返回,如以下代码:
`<input id="wbsnew1" type="image" value="New" onclick="showHideDiv(); return false;" src="images/New.png" />`
also let your function return false也让你的函数返回 false
function showHideDiv() {
document.getElementById("newdata2").style.display = "block";
return false;
}
I suppose that the input tag is located inside some other html element that triggers reloading.我想输入标签位于触发重新加载的其他一些 html 元素内。 Try stopping bubbling:
尝试停止冒泡:
<input id="wbsnew1" type="image" value="New" onclick="showHideDiv(event)" src="images/New.png" />
function showHideDiv(event) {
event.stopPropagation();
document.getElementById("newdata2").style.display = "block";
}
<input type="image" />
elements are used to submit the form (in ASP.NET that equals the postback). <input type="image" />
元素用于提交表单(在 ASP.NET 中相当于回发)。
You have few alternatives:你有几个选择:
<input>
tag to <img />
. <input>
标签替换为<img />
。 The other attributes are the same (except the type
attribute is not needed).type
属性)。return false
to the onclick
handler: <input onclick="showHideDiv(); return false;"
return false
添加到onclick
处理程序: <input onclick="showHideDiv(); return false;"
. Return the false
value from the method itself and add return
keywork to the method从方法本身返回
false
值并将return
键添加到方法中
<input onclick="return showHideDiv();" /> function showHideDiv() { .... return false; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.