【发布时间】:2014-10-01 01:27:09
【问题描述】:
我有一排用于浏览网站的按钮。用户输入填充站点某些页面的信息。在输入信息之前,我想禁用单击导航按钮的功能。会话变量Session["indexloaded"] 和Session["build"] 表示用户是否输入了所需的信息。我目前正在使用这个脚本:
$(document).ready(function () {
document.getElementById("buttontable").onclick = function (){
if ('<%=Session["indexloaded"]%>' === "") {
$("td").attr("onclick", null);
alert("(Alert user that the buttons are disabled until they do a certain action)");
return;
}else if ('<%=Session["build"]%>' === "") {
$("td").attr("onclick", null);
alert("(Alert user that the buttons are disabled until they do a certain action)");
return;
}
}
});
我想禁用以下 onclick 功能。 HTML:
<table id="buttontable">
<tr>
<td class="buttonstyle" onclick="location.href='page1.aspx';" style='text-align: center; width:x%;'>Button 1</td>
<td class="buttonstyle" onclick="location.href='page2.aspx';" style='text-align: center; width:x%;'>Button 2</td>
<td class="buttonstyle" onclick="location.href='page3.aspx';" style="text-align: center; width:x%;">Button 3</td>
<td class="buttonstyle" onclick="location.href='page4.aspx';" style="text-align: center; width:x%;">Button 4</td>
<td class="buttonstyle" onclick="location.href='page5.aspx';" style="text-align: center; width:x%;">Button 5</td>
<td class="buttonstyle" onclick="location.href='page6.aspx';" style="text-align: center; width:x%;">Button 6</td>
</tr>
</table>
现在,如果在会话 === "" 时单击其中一个按钮 (tds),则会显示警报,但我仍将被重定向到该按钮 (td) 的 onclick 函数中标识的目标.如何防止重定向?这可以仅使用 Javascript 或 jQuery 来完成吗?
【问题讨论】:
-
$( "td").unbind( "click" ); -
@GokhanArik 进行了更改,但我仍然遇到与上述相同的问题。警报将显示,但在警报关闭后仍会重定向。
-
@GokhanArik 我也试过了。选定的答案最终对我有用。感谢您的建议
标签: javascript jquery html session onclick