我的结果为MA1
,MA2
....我希望它们为MA0001
,MA0002
...对于两个复选框标签和数据存储在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);
});
});
});
答案 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;
}
参考文献: