如何对所有动态生成的输入强制使用相同的值

时间:2018-10-20 16:26:53

标签: javascript php jquery

大家好,我正在创建一个动态表单,该表单始终为我通过php语句报告的每一代提供相同的值,它本身就是每次生成新输入时我都会写在下面的表单,这往往会创建一个正确但为空,以便用户每次生成新输入时都希望编写不同的内容,相同的输入总是报告相同的值,然后强制使用这种动态形式的性质?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form method="post" action="aggiungi_impianto_alla_campagna.php">


<br>IMPIANTO ID<select name="impianto_id_campagna">
<?php
while($row = mysqli_fetch_assoc($impianti)){ ?>
<option value="<?php echo $row['id'] ;?>"><?php echo $row['cimasa'] ;?></option>
<?php } ?>
</select>

<table id="field">
    <tr>
        <td>Alternatif <span class='num'>1</span></td>
        <td><input type="" name="cliente_id_campagna" value="<?php echo $cliente_id;?>"></td>
        <td><a href="#" class="fieldUp">Row Up</a> | <a href="#" class="fieldDown">Row Down</a></td>
    </tr>
</table>
<button type="button" id="addField">Add Field</button>
<button type="button" id="deleteField">Delete Field</button>


<input class="btn btn-primary" name="submit" type="submit" value="Registra"><br>

</form>

<script id="template" type="text/template">
<tr>
    <td class="number">Alternatif</td>
    <td><input type="text" /></td>
     <td><a href="#" class="fieldUp">Row Up</a> | <a href="#" class="fieldDown">Row Down</a></td>
</tr>
</script>
<script type="text/javascript">
    $( document ).ready(function() {

    $("#addField").click(function( event ) {
        $($("#template").html()).appendTo($("#field tbody")).show( "slow" );
        if( $("td").hasClass("number") ) {
            var i = parseInt($(".num:last").text())+1;
            $( $("<span class='num'> "+ i +" </span>") ).appendTo($(".number")).closest("td").removeClass('number');
        }
        event.preventDefault();
    });

    $("#deleteField").click(function( event ) {
        var lengthRow = $("#field tbody tr").length;
        if( lengthRow > 1 )
            $("#field tbody tr:last").remove();
        event.preventDefault();
    });

    $(document).on("click", ".fieldUp, .fieldDown", function( event ) {
        var row = $( event.target ).closest('tr');
        if( $(this).is(".fieldUp") )
            row.insertBefore(row.prev());
        else
            row.insertAfter(row.next());
        event.preventDefault();
    });

});
</script>

0 个答案:

没有答案