获取自动选中的复选框和下一个输入文本值(以 php 为单位)


Get automatic selected checkbox and next input text value in php

我想在选中时自动获取复选框值和下一个输入文本值。请不要告诉我"给名字像name1,name2等",因为我想从我的数据库中获取数据。

  <?php
    if(isset($_GET['submit'])){
    $amount = $_GET['amount'];
    $checked= $_GET['check'];
    echo "You selected ".$amount.". of ".$checked."";
    }
    ?>
    <form>

    <input type="checkbox" value="Coke" name="check[]">Coke<input type="number" name="amount[]"><br>
    <input type="checkbox" value="Fanta" name="check[]">Fanta<input type="number" name="amount[]"><br>
    <input type="checkbox" value="Water" name="check[]">Water<input type="text" name="amount[]"><br>
    <input type="submit" value='Verder' name="submit">
    </form>

你可以使用 jquery 来做到这一点:

$("input[type=checkbox]" ).on( "click", function(){
    if($(this).is(':checked')){
        var value_of_checkbox = $(this).val();
        var value_of_next_input = $(this).next().val();
        //DO more stuff
    }
});

希望这对你有帮助。你可以通过使用jquery来实现这一点。

$(function(){
    $("input[name='check[]']").click(function(){
        var isChecked = $(this).is(":checked");
        if (isChecked) {
            var nextInputvalue = $(this).next(':input[name="amount[]"]').val();
            alert(nextInputvalue); // this will give the text box value
        }
    });
});