我怎样才能在“.innerHTML”中添加 <g:textField>?

Posted

技术标签:

【中文标题】我怎样才能在“.innerHTML”中添加 <g:textField>?【英文标题】:How can I add <g:textField> in ".innerHTML"? 【发布时间】:2014-10-15 22:05:23 【问题描述】:

我想让 2 输入类型=收音机。当我单击第二个单选按钮时,我想通过标签“”添加四个按钮。如何在“.innerhtml”中添加输入标签? 不要用 JQuery 回答,因为这个系统不能使用 jquery 的 append() 方法。

<script type='text/javascript'>
function appendChildDM()

  var so = document.createElement('span');
  so.innerHTML="<b> Name:  </b><g:textField name="name" value="$name" required="true" maxlength="30" ></g:textField><br><br>";
  so.innerHTML="<b> Company:  </b><g:textField name="company" value="$company" maxlength="30" ></g:textField><br><br>";
  so.innerHTML="<b>E-mail:  </b><g:textField name="email" required="true" value="$email"></g:textField><br><br>";
  so.innerHTML="<b>Phone:  </b><g:textField name="phone" value="$phone"></g:textField>";
 document.getElementById('apch').appendChild(so);

</script>

在“身体”标签中

   <font size="2"><b>  System Contact Information:  <input type="radio" name="sessionUserInfo" value="1"  checked="true"/></b>    <b>Name:</b> $t.memid_name

  <g:hiddenField name="sessionName" value = "$t.memid_name" id="subscribeField"/>
  <g:hiddenField name="sessionOrgName" value = "$t.contract_eorg"/>
  <g:hiddenField name="sessionEmail" value = "$t.memid_email"/>
  <g:hiddenField name="sessionPhone" value = "$t.memid_home"/>
  <g:hiddenField name="memdataId" value = "$t.memdata_id"/>
                    </font>
<br><br>
<div id='apch'>
<font size="2"><p><b> Other Contact Information: <input type="radio" name="sessionUserInfo"  value="2" onclick="appendChildDM();"/></b></p><br>

 </font>
</div>

【问题讨论】:

【参考方案1】:

您使用 = 而不是 +=,使用 = 将替换您现有的 innerhtml。 以下是您所期望的吗?

<script type='text/javascript'>
function appendChildDM()

  var so = document.createElement('span');
  so.innerHTML+="<b> Name:  </b><g:textField name="name" value="$name" required="true" maxlength="30" ></g:textField><br><br>";
  so.innerHTML+="<b> Company:  </b><g:textField name="company" value="$company" maxlength="30" ></g:textField><br><br>";
  so.innerHTML+="<b>E-mail:  </b><g:textField name="email" required="true" value="$email"></g:textField><br><br>";
  so.innerHTML+="<b>Phone:  </b><g:textField name="phone" value="$phone"></g:textField>";
 document.getElementById('apch').appendChild(so);

</script>

【讨论】:

【参考方案2】:
        You can dynamically add en element to the DOM by two ways: by inserting an html code into the parent element or by creating a child element with some properties. In your example the two methods are mixed - therefore senseless.
Method 1. Inserting HTML code:

        var radio = '<input type ="radio" id="' + id + '" value="" />';
        ele.innerHTML = radio;
        document.getElementById(id).onclick = function() alert('this is test'); ;
Method 2. Creating DOM child:

var radioButton = document.createElement('input');
radioButton.type = "radio";
radioButton.id = id;
radioButton.onclick = function() alert('this is test'); ;
ele.appendChild(radioButton);

【讨论】:

【参考方案3】:

您应该将= 替换为+=,并且您在任何地方都放了双引号,这会导致错误。

function appendChildDM()
  var so = document.createElement('span');

  so.innerHTML = /* because the span is empty */ "<b> Name:  </b><g:textField name='name' value='$name' required='true' maxlength='30' ></g:textField><br><br>";
  so.innerHTML += "<b> Company:  </b><g:textField name='company' value='$company' maxlength='30' ></g:textField><br><br>";
  so.innerHTML += "<b>E-mail:  </b><g:textField name='email' required='true' value='$email'></g:textField><br><br>";
  so.innerHTML +="<b>Phone:  </b><g:textField name='phone' value='$phone'></g:textField>";

  // and for the input radio
  so.innerHTML += "<input type='radio' name='theName' /> <input type='radio' name='theName' />";
  document.getElementById('apch').appendChild(so);

【讨论】:

以上是关于我怎样才能在“.innerHTML”中添加 <g:textField>?的主要内容,如果未能解决你的问题,请参考以下文章

我怎样才能重复功能来放置另一个innerHTML?

将控件添加到表格内的innerhtml div

document innerHTML 怎样解决 代码不替换 重复增加问题。

我可以在 innerHTML 中添加样式标签吗?

用js怎样自动循环生成div?

如何在不在 Angular 中添加标签的情况下呈现 innerHTML