Html.EditFor Onchange 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19452784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:35:45  来源:igfitidea点击:

Html.EditFor Onchange Event

htmlasp.net-mvc-4razor

提问by user2739679

I am developing an MVC4 application.
I want to change IsChangedfield in my Model, when model.ExternalVenderNois changed.

我正在开发一个 MVC4 应用程序。 当model.ExternalVenderNo更改时,
我想更改模型中的IsChanged字段。

Below are codes on View page

以下是查看页面上的代码

 <div class="editor-field">
        @Html.EditorFor(model => model.ExternalVenderNo, new { @onchange = "OnChangeEvent();" })
        @Html.ValidationMessageFor(model => model.ExternalVenderNo)
    </div>

and

 function OnChangeEvent()
{
    alert("value is changed");
    @{ Model.IsChanged = true;
    }
}

To check whether OnChangeEventis being called , i put alert. But alertis also not working.
it means OnChangeEventis not beign called.

为了检查是否正在调用OnChangeEvent,我放置了alert。但警报不起作用
这意味着OnChangeEvent没有被调用。

I just want to change IsChangedfield to true, when model.ExternalVenderNois changed.

model.ExternalVenderNo更改时,我只想将IsChanged字段更改为 true 。

Modelhas bool IsChanged

型号bool IsChanged

回答by AthibaN

Try the same with @Html.TextBoxFor

尝试同样的 @Html.TextBoxFor

@Html.TextBoxFor(model => model.ExternalVenderNo, new { onchange = "OnChangeEvent()" })

<script type="text/javascript">
 function OnChangeEvent(){
    alert("value is changed");
    @Model.IsChanged = true;
 }
</script>

or leave Jquery to handle the change event

或让 Jquery 处理更改事件

@Html.EditorFor(model => model.ExternalVenderNo)

<script type="text/javascript">
$('#ExternalVenderNo').change(function(){
    alert('Changed!');
    @Model.IsChanged = true;
});
</script>

回答by Mark

Try adding to attributes

尝试添加到属性

@Html.EditorFor(model => Model.Lengthofside1, new {  htmlAttributes = new { onchange = "OnChangeEvent(this)", @class = "form -control text-right" } })

This worked for me.

这对我有用。

回答by Dilip0165

The following will definitely work for you.

以下内容绝对适合您。

$(document).ready(function()
{
   $('#ExternalVenderNo').on("change", function()
   {
      alert('Changed!');
   });
});

回答by Matt Bodily

try jquery for this

为此尝试 jquery

$('#ExternalVenderNo').change(function(){
    alert('Changed!');
});