如何使用javascript添加@html标签? [英] How do you add @html tags using javascript?

查看:121
本文介绍了如何使用javascript添加@html标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时您需要添加另一个文本框或其他输入类型以获取更多信息。好吧,比如说,A 客户可以有很多地址。当用户填写表单时,当他到达地址时,他可以点击加号为另一个地址添加另一个文本框。所以我做的是这样的:(不知道是否推荐)

There's sometimes that you need to add another textbox or other input type for additional information. Ok, say, A Customer can have many Address. As the user completes the form and as he reach the address he can hit the plus sign to add another textbox for another address. So what I did is something like this: (don't know if it's recommended or not)

Html:

<a href="#" class="add-address">Additional Address</a>
<div class="address-container"></div>

JS:

<script>
$(function() {
  var i = 0;
  var addAddress = function() {
    var strBuilder = '<input type="text" name="Addresses[i].Location" />';
    $('.address-container').append(strBuilder);
    i++;

    return false;
  };
  $('.add-address').click(addAddress);
});
</script>

所以我的问题是:


  1. 可以将文本框添加为 @ Html.EditorFor()

  2. 这真的会是如果我还可以添加 @ Html.ValidationMessageFor(),那还有可能吗?

  1. It is possible to add the textbox as this @Html.EditorFor()?
  2. It would really be great if I can also add in the @Html.ValidationMessageFor(), is it possible?

我正在使用ASP.NET MVC 4; EF Code第一种方法。

I'm using ASP.NET MVC 4; EF Code first approach.

我们非常感谢任何帮助。谢谢。

Any help would be much appreciated. Thanks.

推荐答案

只需使用i作为name属性。

Just use i for name attribute.

name="Addresses[' + i + '].Location"

这与您的模型绑定。

  var i = 0;
  var addAddress = function() {
    var strBuilder = '<input type="text" value="" name="Addresses[' + i + '].Location">';
    $('.address-container').append(strBuilder);
    i++;
    return false;
  };

参见这篇文章对我来说非常有用。

See this post which was much useful for me.

更新

进行验证只需添加此属性以及输入元素。

for validation just add this attributes too along with input element.

data-val-email="The Email field is not a valid e-mail address." 

data-val="true"

这背后的想法是,附加名称属性和验证的正确元素(data-val =true)。
您可以看到已经使用验证的已经工作的页面的渲染html。

An idea behind this is that, appending correct element with name attribute and validation(data-val="true"). You can see rendered html for already working page where you have used validation.

这篇关于如何使用javascript添加@html标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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