更改按钮文本的onclick与条件 [英] Change button text onclick with condition

查看:180
本文介绍了更改按钮文本的onclick与条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是有一个按钮,使用默认值块,并在用户点击它,它会改变文本不阻止,也页面后刷新按钮的文字必须保持不变,如果文本被改变疏通然后刷新后它必须保持疏通。

My aim is to have a button, with default value Block and when the user click on it, it will change the text to Unblock and also after page refresh the text of the button must remain the same, if the text is changed to unblock then after refresh it must remain unblock.

我曾尝试uptil现在是这样的:

What i have tried uptil now is this:

查看code的按钮:

input type="submit" value="@ViewBag.SubmitValue" id="Block" style="color: white; 
background-color: darkred; border-bottom-left-radius: 2px; border-bottom-right-radius: 2px;
border-top-left-radius: 2px; border-top-right-radius: 2px; padding: 4px 4px; 
border: none; padding-bottom:2px "

控制器的ActionResult

Controller ActionResult

public ActionResult Block(int Id, Block block, string userAction)
        {
            if(userAction ==  "Block")
            {
                ViewBag.SubmitValue = "Block";
            }
            if (userAction == "Unblock")
            {
                ViewBag.SubmitValue = "Unblock";
            }
          .....
        }

我在这的ActionResult传球串userAction,userAction的值传递的方法,空,所以按钮上的文字并没有改变,这是显示提交按钮。

I am haivng issue in this passing string userAction in ActionResult, the value of userAction is passing as Null in the Method, so the text of button is not changing and it is showing "Submit" on button

请帮忙

推荐答案

使用名为userAction并将其值设置为任何你需要隐藏输入元素。

use a hidden input element named "userAction" and set its value to whatever you need.

<input type="hidden" name="userAction" value="@ViewBag.SubmitValue">

和默认设置行动的userAction的初始值是这样的:

and for default action set the initialized value of userAction like this:

public ActionResult Block(string userAction = "Block", int Id, Block block){

    //...

}

当你通过什么叫userAction它会被设置为阻止非空

whenever you passed nothing named userAction it will be set to "Block" not null

这篇关于更改按钮文本的onclick与条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆