Textarea和对象

时间:2012-11-29 18:10:20

标签: javascript jquery html asp.net-mvc

是否可以在TextArea中显示List?

"NewList":[{"FirstName":"X","LastName":"Y","Address1":"A1","Address2":"A2","City":"C","State":"S","ZipPostalCode":"12345"}, {"FirstName":"X2","LastName":"Y2","Address1":"A2","Address2":"A3","City":"C2","State":"S2","ZipPostalCode":"12345"}]

根据我的理解,textarea仅用于显示文本而不是HTML,因此<li>不起作用。

基本上,我的要求是在网页上显示上面的列表,并允许用户编辑然后保存它。 有人可以建议怎么做吗?另外,form.SerializeObject如何与我们使用的控件一起使用?

感谢。

1 个答案:

答案 0 :(得分:0)

我建议您浏览每个元素,并为每个属性添加<label><input>

编码示例:

response = [{"FirstName":"X","LastName":"Y","Address1":"A1","Address2":"A2","City":"C","State":"S","ZipPostalCode":"12345"}, {"FirstName":"X2","LastName":"Y2","Address1":"A2","Address2":"A3","City":"C2","State":"S2","ZipPostalCode":"12345"}];
form = document.getElementById('formId');
// Iterate through array items
for(var i=0; i<response.length; i++){
  // Iterate through item attributes
  var item = response[i];
  for(var prop in item){
    // create dom objects
    var label = document.createElement('label');
    var input = document.createElement('input');

    // set dom objects attrs
    input.setAttribute('type', 'text');
    input.setAttribute('name', 'field['+i+']['+prop']');
    label.setAttribute('for', 'field['+i+']['+prop']');

    // append items to form
    form.appendChild(label);
    form.appendChild(input);
  }
}

关于序列化表单,您使用的是this jQuery plugin吗?我是这样,您可能需要使用输入名称来获得有用的结果。

希望这会有所帮助:)