gpt4 book ai didi

javascript - Zerofill 复选框标签和存储在数据库中的数据

转载 作者:行者123 更新时间:2023-11-28 05:42:25 26 4
gpt4 key购买 nike

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

我的 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);
});
});
});

最佳答案

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

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

关于javascript - Zerofill 复选框标签和存储在数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38814909/

26 4 0