ASP.NET MVC-使用JavaScript在运行时添加表单元素 [英] ASP.NET MVC - Adding form elements at runtime with javascript

查看:126
本文介绍了ASP.NET MVC-使用JavaScript在运行时添加表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表单中,我正在如下所示的For循环中添加许多输入元素.

In my form I'm adding a number of input elements in a For Loop, as below.

 @For Each itm As LineItem In Model.LineItems
            Dim lnitm As LineItem = itm
    @<tr>

            <td>@lnitm.Name
            </td>
            <td>@Html.TextBoxFor(Function(model) model.LineItem(lnitm.ID).Value)
            </td>
            <td>@Html.TextBoxFor(Function(model) model.LineItem(lnitm.ID).Currency)
            </td>
        </tr>

        Next

我想在Currency文本框中添加一些jQuery,以便我可以自动完成工作. 在我的其他页面中,我使用了jQuery函数,例如:

I'd like to add some jQuery to the Currency textbox so I can get the autocomplete working. In my other pages I've used a jQuery function such as:

$(function () {
$("#HomeCurrencyID").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/autocomplete/Currencies", type: "POST", dataType: "json",
                data: { searchText: request.term, maxResults: 10 },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.ID + " (" + item.Symbol + ") " + item.Description, value: item.ID , id: item.ID }
                    }))
                }
            })
        },
    });
});

在我知道Input Element的ID的情况下,/在我将元素专门添加到HTML中而不是通过循环添加的情况下,这种方法很好用.

This works fine where I know the ID of the Input Element / where I'm specifically adding the element into the HTML and not adding it via a loop.

如何将上述jQuery功能添加到在For循环中创建的所有货币文本框中?我需要在运行时添加Javascript吗?

How can I add the jQuery functionality above to all currency textbox's created in the For Loop? Do I need to add Javascript at run time?

解决方案

将Function声明更改为一个类名,并将该类添加到文本框中,如下所示;

Changed the Function declaration to a classname and added the class to the textbox as below;

$("#CurrencyClass") 

@Html.TextBoxFor(Function(model) model.LineItem(lnitm.ID).Currency, New With {Key .class = "CurrencyClass"})

感谢您的及时回复!

推荐答案

您需要能够选择所有这些文本框来运行自动完成脚本,为此,您可以添加一个类,然后进行更新脚本中的选择器.

You'll need to be able to select all of these textboxes to run the auto-complete script against, to do this you can add a class, then update the selector in your script.

在视图中:使用new {@class = "yourClass"}(或其VB等效项)作为文本框帮助器的htmlAttributes参数,为所有文本框提供一个类(请参见

In the view: Give all of the textboxes a class using new {@class = "yourClass"} (or its VB equivalent) as the htmlAttributes parameter to the textbox helper (see http://msdn.microsoft.com/en-us/library/ee703535.aspx#Y200).

在脚本中:$("#HomeCurrencyID")替换为$(".yourClass"),它将对所有匹配的元素(即带有yourClass的文本框)运行这些功能.

In the script: Replace $("#HomeCurrencyID") with $(".yourClass"), it will run those functions to all of the matched elements, which will be the textboxes with yourClass.

这篇关于ASP.NET MVC-使用JavaScript在运行时添加表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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