【问题标题】:setting input type in dynamically created form is ignored在动态创建的表单中设置输入类型被忽略
【发布时间】:2016-08-17 13:14:49
【问题描述】:

我正在单击一个按钮以调出一个引导模式,其中包含一个动态创建的表单。我的问题是输入字段始终被视为文本,即使它们设置为其他内容,例如选择、只读或数字。

Here you can see that the inputs have the right type-values, but the inputs are textfields anyway.

此处创建表单(console.log的输出如图):

var data = "";

data += "<form name='altEditor-form' role='form'>";

for(var j = 0; j < columnDefs.length; j++){

  //Outputting input title and input.type 
  console.log(j + ": " + columnDefs[j].title + " - " + columnTypes[j].type)

  data += "<div class='form-group'><div class='col-sm-3 col-md-3 col-lg-3 text-right' style='padding-top:7px;'><label for='" + columnDefs[j].title + "'>" + columnDefs[j].title + ":</label></div><div class='col-sm-9 col-md-9 col-lg-9'><input type='" + columnTypes[j].type + "'  id='" + columnDefs[j].title + "' name='" + columnDefs[j].title + "' placeholder='" + columnDefs[j].title + "' style='overflow:hidden'  class='form-control  form-control-sm' value='" + adata.data()[0][newaData[j].name] + "'></div><div style='clear:both;'></div></div>";
}

data += "</form>";

如您所见,我将输入类型设置为:

<input type='" + columnTypes[j].type + "' ....

然后在此处将表单添加到模态:

$('#altEditor-modal').find('.modal-body').html(data);

我无法理解它。类型怎么可能被完全忽略?

【问题讨论】:

  • 为什么是pre?我怀疑这会使您的标记失效。
  • readonly 不是输入的有效值。您可以添加“readonly”字样。w3schools.com/tags/att_input_readonly.asp
  • @JeremyThille 看起来他正试图将data 的输出呈现为html
  • 这就是我的观点。数据是 html,但 pre 标签将其中和并阻止浏览器解释其中的 html。
  • @JeremyThille 我正在编辑其他人创建的代码,所以这是他工作的残余。我尝试将其更改为 &lt;div&gt; 并同时删除所有标签,但问题是一样的。

标签: javascript jquery forms


【解决方案1】:

您设置了无效的 type 值,浏览器将依次忽略将这些 inputs 视为 type="text":input's default type value

截至本文为止,input 元素的有效 type 值为:

  • button
  • checkbox
  • color
  • date
  • datetime
  • datetime-local
  • email
  • file
  • hidden
  • image
  • month
  • number
  • password
  • radio
  • range
  • reset
  • search
  • submit
  • tel
  • text
  • time
  • url
  • week

readonlyselect 是无效的 type 值。

【讨论】:

  • 我想我对出了什么问题有部分了解。只读字段不应该有type='readonly',而是附加只读属性,如&lt;input type="text" readonly&gt; - 这是正确的吗?
  • @BaconPancakes 是的
  • @MageshKumaar 谢谢,就&lt;input type="select"&gt; 而言,它应该是&lt;select id=""&gt;&lt;option&gt;OPTION1&lt;/option&gt;&lt;/select&gt;
  • @BaconPancakes,是的。 Read.
猜你喜欢
  • 2015-08-12
  • 2018-02-04
  • 2020-03-11
  • 2018-04-12
  • 1970-01-01
  • 2017-05-21
  • 2018-09-14
  • 1970-01-01
  • 2015-01-19
相关资源
最近更新 更多