如何从OnChange事件中捕获文本框的值

如何从OnChange事件中捕获文本框的值

本文介绍了如何从OnChange事件中捕获文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#MVC应用程序中,我有一系列这样生成的文本框...

In my C# MVC app, I have a series of textboxes that are generated as such...

@foreach (object item in items)
{
    @Html.TextBox(....)
}

渲染的结果是一系列看起来像这样的文本框....

The rendered result is a series of text boxes that look like this....

<input class="item-quantities valid" data-bomid="1939" data-rid="2054" id="AddedItemIDs_1939_" name="AddedItemIDs[1939]" onchange="ChangeItemQuantity(156,78)" onkeypress="return isNumberKey(event)" type="text" value="7" aria-invalid="false">

<input class="item-quantities valid" data-bomid="1940" data-rid="1055" id="AddedItemIDs_1940_" name="AddedItemIDs[1940]" onchange="ChangeItemQuantity(159,90)" onkeypress="return isNumberKey(event)" type="text" value="1">

现在,我需要一个javascript/jquery函数,在其中可以捕获三个值:

Now, I need a javascript / jquery function in which I can capture three values:

  1. bomid
  2. rid
  3. 文本框的新值

当文本框失去焦点(跳出等)时,我就很好地获取了值

I am just fine capturing the value when the textbox loses focus (tab out, etc.)

我已经尝试过此操作(在文本框中为onchange ="ChangeItemQuantity()"),但是由于某些原因,我永远无法触发此事件.另外,我宁愿不这样做,因为那样我就不得不对分配给文本框的类保持严格的态度.

I have tried this (WITHOUT onchange="ChangeItemQuantity()" in the textbox), but for some reason I can never get this event to fire. Plus, I'd rather NOT do it this way, because then I am forced to be rigid on what classes I assign to the textboxes....

    $(function () {
        $('.item-quantities.valid').change(function () {
            var value = $(this).val();
            var bomid= $(this).data('bomid');
            var rid= $(this).data('rid');
        });
    });

我已经尝试过此操作(在文本框中为onchange ="ChangeItemQuantity(159,90)").我会这样做,因为它使我可以灵活处理文本框中的类...

And I have tried this (WITH onchange="ChangeItemQuantity(159,90)" in the textbox). I would RATHER do it this way, as it allows me to be flexible with the classes in the text box...

function ChangeItemQuantity(bomid, rid) {
    // no idea how I would capture the value here.
}

推荐答案

向函数添加另一个参数以将元素传递到其中并获取它的value:

Add another argument to the function to pass the element in and get it's value:

 onchange="ChangeItemQuantity(156,78, this)"

function ChangeItemQuantity(bomid, rid, el) {
    alert(el.value)
}

这篇关于如何从OnChange事件中捕获文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:38