zerofill复选框标签和存储在DB中的数据

时间:2016-08-07 14:05:28

标签: javascript php jquery

我的结果为MA1MA2 ....我希望它们为MA0001MA0002 ...对于两个复选框标签和数据存储在DB中。

我的PHP:

<?php
        for ($i = 1; $i < 102; $i++):

            echo '<div class="each_checkboxes">';

            echo '<label class="contact" for="checkbox'.$i.'"></label>';
            echo '<input type="checkbox" name="tape[]" id="checkbox'.$i.'" value=""/>';

            echo '</div>';

        endfor;
    ?>

js:

$(document).ready(function () {
    $('#method').change(function () {
        var method = $('option:selected').val(),
            text = "";

        if (method == "CSBSFSR20003") {
            text = "MA";
        } else if (method == "CSBSAPP20029") {
            text = "SAS";
        }

        $('.contact').each(function (i) {
            var value = text + (++i);
            $(this).text(value);
            $('#' + $(this).attr('for')).val(value);
        });
    });
});

2 个答案:

答案 0 :(得分:0)

试试这个

<?php
        for ($i = 1; $i < 102; $i++):
             $ii=$i;
            if($i<1000)
                $ii="0".$ii;
            if($i<100)
                $ii="0".$ii;
            if($i<10)
                $ii="0".$ii;
            echo '<div class="each_checkboxes">';

            echo '<label class="contact" for="checkbox'.$ii.'"></label>';
            echo '<input type="checkbox" name="tape[]" id="checkbox'.$ii.'" value=""/>';

            echo '</div>';

        endfor;
    ?>

答案 1 :(得分:0)

尝试这个(使用几种不同的算法填充数字:

<强> PHP:

<?php
    function addZeros($num){
        if (strlen($num)==3 ){
            $num = '0' . $num;
        }elseif (strlen($num)==2{
            $num = '00' . $num;
        }elseif (strlen($num)==1{
            $num = '000' . $num;
        }
        return $num;
    }
    for ($i = 1; $i < 102; $i++):
        $num = addZeros($i);
        echo '<div class="each_checkboxes">';

        echo '<label class="contact" for="checkbox'.$num.'"></label>';
        echo '<input type="checkbox" name="tape[]" id="checkbox'.$num.'" value=""/>';

        echo '</div>';

    endfor;
?>

<强>的javascript:

$(document).ready(function () {
    $('#method').change(function () {
        var method = $('option:selected').val(),
            text = "";

        if (method == "CSBSFSR20003") {
            text = "MA";
        } else if (method == "CSBSAPP20029") {
            text = "SAS";
        }

        $('.contact').each(function (i) {
            var num = addZeros(++i);
            var value = text + num;
            $(this).text(value);
            $('#' + $(this).attr('for')).val(value);
        });
    }); //END #method.change
}); //END document.ready

function addZeros(num) {
  if (num<=9999) { num = ("000"+num).slice(-4); }
  return num;
}

参考文献:

https://stackoverflow.com/a/10073761/1447509

http://php.net/manual/en/function.strlen.php