【问题标题】:Create popup form dynamically by selecting a checkbox通过选中复选框动态创建弹出表单
【发布时间】:2015-08-08 08:16:30
【问题描述】:

function msg()
{
  if (document.getElementById('c1').checked);
    alert("suggest an update");
  }
<input class="form-control" type="checkbox" id="c1" onclick="msg()" name="like"/>

【问题讨论】:

  • 需要更多解释你想要做什么。就像其他网站中的示例一样
  • 你可以使用像jQuery这样的javascript库

标签: javascript php wordpress


【解决方案1】:

你可以用不同的方式弹出表单,其中之一是使用 jQuery

这是一个example,可以做你想做的事。

【讨论】:

    【解决方案2】:

    如果你使用 Angularjs,我强烈推荐ngDialog。这是我最喜欢的弹出窗口类型。 您只需创建一个包含弹出内容的 html 文件,并在选中复选框后创建对话框的实例(您可以使用 ngChange 观察值的变化)

    【讨论】:

      【解决方案3】:

      下面将生成一个带有一个文本输入元素的表单,很容易扩展它以包含许多其他元素。不能给出任何 jquery 的例子,因为我不使用它。

          <script type='text/javascript' charset='utf-8'>
              function msg(e) {
                  var id='bob';
                  var el=e.target || e.srcElement;
                  if( el.checked ){
      
                      var cont=document.getElementsByTagName('body')[0];                  
                      var form=addNode('form',{id:id},cont);
                          form.style.width='500px';
                          form.style.height='500px';
                          form.style.zIndex=10;
                          form.style.border='1px solid black';
                      addNode('input',{type:'text',name:'fred'},form);
                  }else{
                      if( document.getElementById(id) ) document.getElementById(id).parentNode.removeChild( document.getElementById(id) );
                  }
              }
              function addNode( t, a, p ) {
                  var el = ( typeof( t )=='undefined' || t==null ) ? document.createElement( 'div' ) : document.createElement( t );
                  for( var x in a ) if( a.hasOwnProperty( x ) && x!=='innerHTML' ) el.setAttribute( x, a[ x ] );
                  if( a.hasOwnProperty('innerHTML') ) el.innerHTML=a.innerHTML;
                  if( p!=null ) typeof( p )=='object' ? p.appendChild( el ) : document.getElementById( p ).appendChild( el );
                  return el;
              }
      /*
      The addNode function:
      First argument is the type of element you wish to add.
      Second argument is an object literal containing properties for the element you wish to set.
      Third argument is the parent node to which you wish to append the new element/node.
      */
          </script>
      
      
      html
      ----
      <input class="form-control" type="checkbox" id="c1" onclick="msg(event)" name="like"/>
      

      【讨论】:

      • 非常感谢,但是当我取消选中复选框时它应该关闭
      • 你确定可以添加吗?
      猜你喜欢
      • 2018-05-17
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 2016-06-16
      • 2019-12-11
      • 1970-01-01
      相关资源
      最近更新 更多