【问题标题】:How to disable/ hide all buttons after one click on any button (cshtml)单击任何按钮后如何禁用/隐藏所有按钮(cshtml)
【发布时间】:2019-02-15 06:52:16
【问题描述】:

我想知道一旦用户单击任何按钮后如何禁用/隐藏所有按钮。我只能使用 c# 或 html。我在 Javascript 中找到了解决方案,但我无法使用它。 (由于空间不足,我没有上传我的 razor c# 代码)

我正在创建一个程序,允许用户投票给任何一位候选人。一旦用户点击并选择了一个候选人,将显示投票结果,用户不应再次投票。

<!DOCTYPE html>
<html>
<head>
    <title>Elections</title>
</head>
<body>
    <form id="form1" method="post">
        <div>
            <table>
                <tr>
                    <td>Harry</td>
                    <td><input id="Harry" name="submit" type="submit" value="Vote Harry" /></td>

                </tr>
                <tr>
                    <td>John</td>
                    <td><input id="John" name="submit" type="submit" value="Vote John" /></td>
                </tr>
                <tr>
                    <td>Bryan</td>
                    <td><input id="Bryan" name="submit" type="submit" value="Vote Bryan" /></td>
                </tr>
                <tr>
                    <td>Jack</td>
                    <td><input id="Jack" name="submit" type="submit" value="Vote Jack" /></td>
                </tr>
            </table>
        </div>
        <div>
            @if (result != "")
            {
                <p>@result</p>
            }

            <!--Ensure that user has voted before showing the vote results-->
            @if (voteCheck == true)
            {
                <p>Total Votes: @Session["totalVotes"]</p> <!--using session allows values to be kept even after button click-->
                <p>      Harry: @Session["Harry"]</p>
                <p>       John: @Session["John"]</p>
                <p>      Bryan: @Session["Bryan"]</p>
                <p>       Jack: @Session["Jack"]</p>
            }
        </div>
    </form>
</body>
</html>

【问题讨论】:

  • 我假设该页面已提交,并且在下次查看时需要禁用按钮:更新您的表单处理程序以设置一个标志以禁用所有按钮,并包括对该标志的检查以隐藏按钮。

标签: html asp.net razor


【解决方案1】:

您需要action="youraction/controller here" 形式的操作属性 在您的操作中,您编写了一些显示值的 C# 代码返回列表(“无”或“”) 在cshtml中添加style="display:@display"

<input id="Harry" name="submit" type="submit" style="display:@display" value="Vote Harry" />

【讨论】:

    【解决方案2】:

    您可以简单地添加一个三元运算符来检查会话对象并相应地设置disabled 属性。

    <input id="Harry" @(Session["Harry"] != null ? "disabled" : "") name="submit" type="submit" value="Vote Harry" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2022-10-22
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 2021-01-23
      相关资源
      最近更新 更多