gpt4 book ai didi

javascript - ASP.NET onClientClick 返回 false 不起作用

转载 作者:行者123 更新时间:2023-12-02 23:45:18 24 4
gpt4 key购买 nike

我想在单击按钮时隐藏 div,并在单击不同的按钮时显示它们。遗憾的是,单击图像按钮时总是会出现回发。是什么导致了这个问题?

这是我的后端代码

protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 10; i++)
{
HtmlGenericControl divTest = new HtmlGenericControl("div");
divTest.Attributes.Add("class", "divClass");
divTest.Attributes.Add("ID", "myDIV");
divTest.InnerText = "Div" + i;
form1.Controls.Add(divTest);

ImageButton collapseButton = new ImageButton();
collapseButton.ImageUrl = "~/images/minus.png";
collapseButton.Attributes.Add("OnClientClick", "myHideFunction(); return false;");
collapseButton.Height = 20;
collapseButton.Width = 20;
divTest.Controls.Add(collapseButton);

ImageButton expandButton = new ImageButton();
expandButton.ImageUrl = "~/images/plus.png";
collapseButton.Attributes.Add("OnClientClick", "myShowFunction(); return false;");
expandButton.Height = 20;
expandButton.Width = 20;
form1.Controls.Add(expandButton);
}
}

这是我的前端代码

CSS

<style>
#myDIV {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top: 20px;
}
</style>

Javascript

<script>

function myShowFunction() {
var x = document.getElementById("myDIV");
x.style.display = "block";
}

function myHideFunction() {
var x = document.getElementById("myDIV");
x.style.display = "none";
}
</script>

最佳答案

添加 onclientclick 按钮:

collapseButton.OnClientClick = "return myHideFunction();");

您需要从您的方法中返回 false,如下所示:

<script>

function myShowFunction() {
var x = document.getElementById("myDIV");
x.style.display = "block";
return false;
}

function myHideFunction() {
var x = document.getElementById("myDIV");
x.style.display = "none";
return false;
}
</script>

关于javascript - ASP.NET onClientClick 返回 false 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55885950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com