【问题标题】:Adding new input fields on check box ticked在复选框上添加新的输入字段打勾
【发布时间】:2021-12-29 09:52:37
【问题描述】:

我有一个包含 3 个文本字段的表格 我想在单击复选框时添加相同的文本字段我有以下代码 我怎么能用 php 和 javascript 做到这一点

 echo "<td>Screen ".$i."</td>";
                       echo "<td><input type='text' id='filmname".$k."' name='filmname".$k."'value='".$prefilm."'></td>";
                       echo "<td><input type='text' id='Language".$k."' name='Language".$k."'value='".$prelang."'></td>";
                       echo "<td><input type='text' id='showtime".$k."' name='showtime".$k."'value='".$prescreen."'></td>";
                       
 echo "<td ><input type='checkbox' class='Checkbox' id='addshow".$k."' autocomplete='off'
                       name='addshow".$k."' value='addshow' onclick='addshow(".$k.")</td>";                       
                       

【问题讨论】:

    标签: javascript php html


    【解决方案1】:

    如果您能在复选框被选中时阐明您想要添加哪些文本字段,那就太好了。

    两个想法:

    1. 您可以在创建表格的同时创建文本字段,然后使用 CSS 和 JavaScript 隐藏/显示它们。 (首选)
    2. 为您的复选框添加一个点击监听器,并使用 JS 和 document.createElement('input') Learn more here 动态创建您的文本字段

    第二个选项的代码示例:

    const check = document.querySelector('input[type=checkbox]');
          check.addEventListener('click', createTextInput);
    
    function createTextInput() {
      const target = document.querySelector(YOUR TARGET SELECTOR);
    
      const input = document.createElement('input'); 
            input.setAttribute('type', 'text');
            input.addEventListener(OPTIONAL IF YOU WANT)
    
      target.appendChild(input); // this adds the new input into your target
    }
    
    

    【讨论】:

    • 你能解释一下第二种方法吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多