1

我正在创建一个表单,并且我添加了一个按钮来创建动态文本框 bt 当我在创建动态文本框后提交表单时,我没有从文本框中得到任何输出

    echo  '<form action="" method="post">';
    echo '<INPUT size=70% TYPE="hidden" VALUE="AIzaSyxxxxxxxxxx" NAME="apiKey">';
    echo "<INPUT size=70% TYPE = 'hidden' VALUE='xx' NAME = 'registrationIDs'>";
    echo '<table class="table" name ="table">';

 for($i=0;$i<$counter;$i++){
   $srno=$i+1;
   echo "<tr name='input-holder'>";
       echo " <td><input type='text' value=' $medicsarray[$i] - $qtyarray[$i]'    
       name='medic'/>"; echo "</td>"; 
       echo "<td><input type='text' name='add[]'  /> </td> ";
       echo "</tr>";
     }
    echo "</table>";
           }}
    ?>
     <table class="table">
      <tr><td><input type='button' name='addmedic' value='add new input box' /></td> 
      <td></td></tr>
      <tr><td>TOTAL:</td><td><input type='text' id='results' value="0"></td></tr>
     </table>
     <input type="submit" value="Send Notification"/>
     </form>    

我的 JavaScript 代码

    function bindFunctions() {
    var sub = document.getElementsByName("addmedic")[0];
    sub.onclick = onClickFunction;
    var input = document.getElementsByName("add[]");
       for (var i = 0; i < input.length; i++) {
           if (input[i].type == "text") {
               input[i].onblur = onBlurFunction;
              }
           }
       }

    bindFunctions();

    function onBlurFunction() {
      var ttl = 0;
      var input = document.getElementsByName("add[]");
      for (var i = 0; i < input.length; i++) {
           if (input[i].type == "text") {
               ttl += Number(input[i].value);
             }
           }
      res =document.getElementById("results");
      res.value=ttl;
    }

    function onClickFunction() {

    var inputHolder = document.getElementsByName('table');
    var a= inputHolder.length;
    a=a-1;

    inputHolder[a].innerHTML +="<tr name='input-holder'><td><input type='text'           

    name='medic' placeholder='medicine name and qty' /></td><td><input type='text'  

    name='add[]'/></td></tr>";
    bindFunctions();
    }
    </script>
    <pre>
      <?php print_r($_POST); ?>
    </pre>

当我不单击添加文本框按钮时输出 [添加] => 数组([0] => 23 [1] => 23)

4

3 回答 3

1

清理代码后它可以工作,所以我不知道问题出在哪里,也缺少某些部分代码。这是一个格式更好的 html 和 php,其中详细描述了 JavaScript。分析变化。

<form action="" method="post">
    <INPUT size='70%' TYPE='hidden' VALUE='AIzaSyxxxxxxxxxx' NAME='apiKey'/>
    <INPUT size='70%' TYPE='hidden' VALUE='xx' NAME='registrationIDs'/>
    <table id="medics" class="table">
    <?php for ( $i=0; $i<$counter; $i++ ) {
        echo    "<tr name='input-holder'>
                    <td><input type='text' value='$medicsarray[$i] - $qtyarray[$i]' name='medic[]'/></td>
                    <td><input type='text' name='add[]'/></td>
                </tr>";
    } ?>
    </table>
    <table class="table">
        <tr><td><input id="addmedic" type='button' name='addmedic' value='add new input box'/></td><td></td></tr>
        <tr><td>TOTAL:</td><td><input type='text' id='results' value="0"></td></tr>
    </table>
   <input type="submit" value="Send Notification"/>
</form>

<script>

// there is only one 'addmedic' button, so use the id attribute for easier access
document.getElementById("addmedic").onclick = onClickFunction;

var input = document.getElementsByName("add[]");
for ( var i = 0; i < input.length; i++ ) {
    // no need for input[i].type == "text" check here, all inputs with name='add[]' are text type
    input[i].onblur = onBlurFunction;
}

function onClickFunction() {
    // it is better to play by the rules, innerHTML on table will destroy all event handlers, and will clear the input values
    // create the new row, set attributes, add html to that row only
    var row = document.createElement('tr');
    row.setAttribute( 'name', 'input-holder' );
    row.innerHTML = "<td><input type='text' name='medic[]' placeholder='medicine name and qty' /></td><td><input type='text' name='add[]'/></td>";

    // the table has id attribute id="medics" for easier access
    var table = document.getElementById('medics');
    // get the parent of last row ( probably 'tbody' ) and append new row
    table.rows[ table.rows.length - 1 ].parentNode.appendChild(row);

    // get last textbox with name='add[]', and set onblur event handler
    var input = document.getElementsByName("add[]");
    input[ input.length - 1 ].onblur = onBlurFunction;

}

function onBlurFunction() {
    var ttl = 0;
    var input = document.getElementsByName("add[]");
    for (var i = 0; i < input.length; i++) {
        // check if value is not a number
        if ( !isNaN(input[i].value) ) ttl += Number(input[i].value);
    }
    document.getElementById("results").value = ttl;
}
</script>

<?php print_r( $_POST ); ?>
于 2013-06-10T22:36:21.310 回答
0

感谢您的所有回复。我只是将表格标签移到表格上方,然后得到了所需的输出。

于 2013-06-12T20:17:20.160 回答
0

为了避免Danijel的代码出错,可以修改为

<form action="" method="post">
    <INPUT size='70%' TYPE='hidden' VALUE='AIzaSyxxxxxxxxxx' NAME='apiKey'/>
    <INPUT size='70%' TYPE='hidden' VALUE='xx' NAME='registrationIDs'/>
    <table id="medics" class="table">
    <?php for ( $i=0; $i<$counter; $i++ ) {
        echo    "<tr name='input-holder'>
                    <td><input type='text' value='' name='medic[]'/></td>
                    <td><input type='text' name='add[]'/></td>
                </tr>";
    } ?>
    </table>
    <table class="table">
        <tr><td><input id="addmedic" type='button' name='addmedic' value='add new input box'/></td><td></td></tr>
        <tr><td>TOTAL:</td><td><input type='text' id='results' value="0"></td></tr>
    </table>
   <input type="submit" value="Send Notification"/>
</form>

<script>

// there is only one 'addmedic' button, so use the id attribute for easier access
document.getElementById("addmedic").onclick = onClickFunction;

var input = document.getElementsByName("add[]");
for ( var i = 0; i < input.length; i++ ) {
    // no need for input[i].type == "text" check here, all inputs with name='add[]' are text type
    input[i].onblur = onBlurFunction;
}

function onClickFunction() {
    // it is better to play by the rules, innerHTML on table will destroy all event handlers, and will clear the input values
    // create the new row, set attributes, add html to that row only
    var row = document.createElement('tr');
    row.setAttribute( 'name', 'input-holder' );
    row.innerHTML = "<td><input type='text' name='medic[]' placeholder='medicine name and qty' /></td><td><input type='text' name='add[]'/></td>";

    // the table has id attribute id="medics" for easier access
    var table = document.getElementById('medics');
    // get the parent of last row ( probably 'tbody' ) and append new row
    table.rows[ table.rows.length - 1 ].parentNode.appendChild(row);

    // get last textbox with name='add[]', and set onblur event handler
    var input = document.getElementsByName("add[]");
    input[ input.length - 1 ].onblur = onBlurFunction;

}

function onBlurFunction() {
    var ttl = 0;
    var input = document.getElementsByName("add[]");
    for (var i = 0; i < input.length; i++) {
        // check if value is not a number
        if ( !isNaN(input[i].value) ) ttl += Number(input[i].value);
    }
    document.getElementById("results").value = ttl;
}
</script>

<?php print_r( $_POST ); ?>
于 2013-09-09T05:32:29.177 回答