asp.net-mvc 在 mvc3 中 @Html.TextboxFor 的 onchange 事件上更改标签文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17874849/
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-09-08 03:02:59  来源:igfitidea点击:

change text of label on onchange event of @Html.TextboxFor in mvc3

javascriptasp.net-mvcasp.net-mvc-3

提问by user2450398

I am working with MVC3 Razor. I want to change the text of label on onchange event of @Html.TextboxFor.

我正在使用 MVC3 Razor。我想在@Html.TextboxFor 的 onchange 事件上更改标签的文本。

Here is my code for what I am trying:

这是我正在尝试的代码:

On View:

在视图中:

@Html.TextBoxFor(x => x.ItnScanCaseCode, new { @onchange = "event();" })

JavaScript:

JavaScript:

function event() {
    document.getElementById('lblSelectedProductName').value ="sam";
}

But it's not working.

但它不起作用。

回答by Janki

Use this jquery syntax its work fine.

使用此 jquery 语法可以正常工作。

@Html.TextBoxFor(x => x.ItnScanCaseCode, new { @id="txtid",@onchange = "onchangeevent();" })

function onchangeevent(){
        $('#txtid').val('sam');
    }

回答by Brendan Vogt

This can be accomplished with JavaScript, but I am more comfortable to use jQuery. I like to separate my HTML and JavaScript and rather listen for the on change event after all the controls have loaded into the DOM.

这可以用 JavaScript 来完成,但我更喜欢使用jQuery. 我喜欢将 HTML 和 JavaScript 分开,而是在所有控件加载到 DOM 后监听 on change 事件。

Your HTML markup for the text box could look something like this. I added my own label element for testing purposes.

文本框的 HTML 标记可能如下所示。我添加了自己的标签元素以进行测试。

<input id="ItnScanCaseCode" name="ItnScanCaseCode" type="text" value="" />
<label id="lblSelectedProductName">Test Label Text</label>

And your jQuery script to add the on change event listener:

以及用于添加 on change 事件侦听器的 jQuery 脚本:

$(document).ready(function () {
     $('#ItnScanCaseCode').change(function () {
          $('#lblSelectedProductName').text('sam');
     });
});

If you need to add this label text changing to a separate function method then do it like this, but the above way is sufficient for me.

如果您需要将此标签文本更改添加到单独的函数方法中,请这样做,但上述方式对我来说已经足够了。

$(document).ready(function () {
     function yourEventMethod() {
          $('#lblSelectedProductName').text('sam');
     };

     $('#ItnScanCaseCode').change(function () {
          yourEventMethod();
     });
});

I hope this helps.

我希望这有帮助。

回答by Sridhar Narasimhan

The problem is with event name try with different event name it will work.

问题在于事件名称尝试使用不同的事件名称它会起作用。

@Html.TextBoxFor(x => x.ItnScanCaseCode, new { @onchange = "onchangeevent();" })

function onchangeevent(){
        document.getElementById('lblSelectedProductName').value ="sam";
    }

Regards

问候

回答by Debendra Dash

you can try this

你可以试试这个

  @Html.TextBoxFor(model => model.Image, new { type = "file",
                       @onchange = "show(this);" })

Where my JavaScript is this

我的 JavaScript 在哪里

<script type="text/javascript">

   function show(input) {
      if (input.files && input.files[0]) {
      var filerdr = new FileReader();
      filerdr.onload = function (e) {
         $('#user_img').attr('src', e.target.result);
      }
      filerdr.readAsDataURL(input.files[0]);
   }
}
</script>