简体   繁体   English

单击按钮上的JS函数以刷新页面

[英]Calling a JS function on a button click refreshes the page

I have a ASP page which has a JS function that is being called on a HTML input button click, but every time on the button click the page is reloaded and goes back to the initial state. 我有一个ASP页面,该页面具有一个JS函数,该函数在HTML输入按钮单击时被调用,但是每次单击该按钮时,页面都会重新加载并返回到初始状态。

I have disabled caching both on server and client side, using these tags 我已使用这些标签在服务器和客户端禁用了缓存

<%
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
%>

and

<meta http-equiv="Expires" CONTENT="0"/>
<meta http-equiv="Cache-Control" CONTENT="no-cache"/>
<meta http-equiv="Pragma" CONTENT="no-cache"/>   

I tried to figure out what is the problem which is causing the page to refresh on a button click, rather than calling a JS function, but no dice. 我试图找出是什么原因导致页面在单击按钮时刷新,而不是调用JS函数,但是没有骰子。


As asked for here is the button code 如要求的是按钮代码

   <button class="textinput" id ="reloadData" onclick="reloadData()" title="Reload Data">Refresh</button>

and the function called 而函数称为

            function reloadData() {
            var DPS = document.getElementById("datepickerStart").value;
            var DPE = document.getElementById("datepickerEnd").value;

            var startDP = new Date(DPS);
            var endDP = new Date(DPE);

            var startDate = (startDP.format("isoDateTime")).replace(/:/g, '\\:');
            var endDate = (endDP.format("isoDateTime")).replace(/:/g, '\\:');

            layer.redraw();
            }

I don't think this has to do something with the function called because it is just readjusting the values in the filter. 我认为这与调用函数无关,因为它只是重新调整了过滤器中的值。

The button is likely causing a postback. 该按钮可能导致回发。 Does it exist in a form? 它以某种形式存在吗?

Have the javascript function return false to prevent the postback. 让javascript函数返回false以防止回发。

Reuse of reloadData in <... id ="reloadData" onclick="reloadData()" ...> may be problematic in some browsers, though you wouldn't expect it. 再利用reloadData<... id ="reloadData" onclick="reloadData()" ...>可能会在某些浏览器有问题,虽然你不会指望它。

In any case, try attaching the onclick handler in javascript rather than in the HTML, as follows: 无论如何,请尝试在javascript(而不是HTML)中附加onclick处理程序,如下所示:

HTML: HTML:

<button class="textinput" id="reloadData" title="Reload Data">Refresh</button>

Javascript: Javascript:

window.onload = function(){
    var reloadDataButton = document.getElementById('reloadData');
    var DPS = document.getElementById("datepickerStart");
    var DPE = document.getElementById("datepickerEnd");
    if(reloadDataButton && DPS && DPE){
        reloadDataButton.onclick = function(){
            var startDP = new Date(DPS.value);
            var endDP = new Date(DPE.value);

            var startDate = (startDP.format("isoDateTime")).replace(/:/g, '\\:');
            var endDate = (endDP.format("isoDateTime")).replace(/:/g, '\\:');

            layer.redraw();
            }
        };
    }
};

(Merge with existing window.onload handler if necessary) (如有必要,与现有的window.onload处理程序合并)

If that doesn't fix it, and the button is genuinely not in a form, then the problem must lie in layer.redraw(); 如果那不能解决问题,并且按钮确实不是形式,则问题必须出在layer.redraw(); (or a window.onerror handler). (或window.onerror处理程序)。 Try commenting out layer.redraw(); 尝试注释掉layer.redraw(); and see what happens. 看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM