【问题标题】:How can I add <g:textField> in ".innerHTML"?我怎样才能在“.innerHTML”中添加 <g:textField>?
【发布时间】: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>

【问题讨论】:

    标签: javascript grails innerhtml appendchild


    【解决方案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);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-28
          • 2018-07-15
          • 1970-01-01
          • 2014-03-20
          • 2017-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多