简体   繁体   English

单击时禁用按钮,但无法将参数传递给控制器

[英]Disable button on click but cannot pass parameter to controller

I have a button, which when clicked I want to disable. 我有一个按钮,单击后要禁用它。

<input type="submit" value="Save" class="btn btn-default" name="submit" style="float: right; position: relative; right: -18px;" />

The value "Save" needs to pass from the button to the controller: 值“保存”需要从按钮传递到控制器:

 public ActionResult EditCall(ModelCallDetails call, string submit)

but, obviously when I put an OnClick event such as disable, the parameter is not pushing to the controller. 但是,很明显,当我放置诸如Disable之类的OnClick事件时,该参数未推送到控制器。

Is there anyway of actually passing the value to the controller, the form is still posting the model info. 无论如何,实际上是否会将值传递给控制器​​,表单仍在发布模型信息。

Use onsubmit event. 使用onsubmit事件。

Example : 范例:

<form action="" method="post" onsubmit="return submitForm(this);">  
    <input type="submit" value="Save" class="btn btn-default" name="submit" style="float: right; position: relative; right: -18px;" /> 
</form>  

<script type="text/javascript">  
    function submitForm(th) {   
        th.submit.disabled = true;  
        return true;
    }  
</script>  

You can test in the below snippet that when the button becomes disable, the submit event isn't called anymore. 您可以在下面的代码段中测试,当按钮变为禁用状态时,不再调用Submit事件。 I only changed return true to return false because I shouldn't post to stackoverflow. 我只将return true更改为return false因为我不应该发布到stackoverflow。 It's just to show you that the event isn't callable anymore after having its submit button setted to disabled=true . 只是为了向您显示该事件的提交按钮设置为disabled=true之后,该事件不再可调用。

 <form action="" method="post" onsubmit="return submitForm(this);"> <input type="submit" value="Save" class="btn btn-default" name="submit" /> </form> <script type="text/javascript"> function submitForm(th) { th.submit.disabled = true; console.log("passed"); return false; } </script> 

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

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