简体   繁体   English

为什么当我在我的 aspx 页面中的 html 图像点击事件中调用 JavaScript 函数时,我的网页正在刷新?

[英]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:你有几个选择:

  1. (best) Replace the <input> tag to <img /> . (最好)将<input>标签替换为<img /> The other attributes are the same (except the type attribute is not needed).其他属性相同(除了不需要type属性)。
  2. Add return false to the onclick handler: <input onclick="showHideDiv(); return false;"return false添加到onclick处理程序: <input onclick="showHideDiv(); return false;" . .
  3. 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.

相关问题 为什么Click事件没有调用我的函数 - why Click event is not calling my function 为什么在 JavaScript 中调用异步函数时我的 HTML 值没有改变? - Why is my HTML value not changing when calling async function in JavaScript? 我可以正确调用我的JavaScript函数吗? - Am I calling my JavaScript Function correctly? 为什么刷新页面后我的网页上看不到我的Ajax更新? - Why do i not see my Ajax update on my webpage with refreshing the page? 启动网页时,jquery .click()未注册事件侦听器 - jquery .click() is not registering an event listener when I start my webpage 当我单击关闭表单的按钮时,为什么我的 HTML 网页会刷新 - Why would my HTML webpage refresh when i click a button that closes a form 为什么在单击使用JavaScript的输入按钮时,我的asp.net页已刷新? - Why on click of a input button using JavaScript i am getting my asp.net page refreshed? 为什么当我的页面加载而不是单击我的单选按钮时触发我的 onchange 事件? - Why does is my onchange event triggered when my page loads instead of when I click my radio button? 当我在 Javascript 上使用点击事件时,如何防止我的图像消失? - How do I prevent my image from disappearing when I use a click event on Javascript? 为什么我的 HTML 中的图像不会随着我的 JavaScript 函数而改变? - Why won't the image in my HTML change with my JavaScript function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM