我想创建一个具有多个后续选择字段的表单。这是一个包含3个字段的数组:ID, Name, followingID。FollowingID匹配一个ID,这样我们就可以找出层。
小规模示例:0 = ID, 1 = Name, 2 = followingID
Array
(
[0] => Array
(
[0] => 1
[1] => Auto’s, werktuigkundigen, technici, ingenieurs
[2] => 0
)
[1] => Array
(
[0] => 2
[1] => Bewaking, leger, politie
[2] => 1
)
[2] => Array
(
[0] => 3
[1] => Bouw, montage
[2] => 1
)
[3] => Array
(
[0] => 4
[1] => Commercieel, winkel, inkoop en verkoop
[2] => 2
)
[4] => Array
(
[0] => 5
[1] => Financiën, bank, verzekering
[2] => 2
)
[5] => Array
(
[0] => 6
[1] => Gezondheidszorg, paramedici, laboratorium
[2] => 3
)
[6] => Array
(
[0] => 7
[1] => Gezondheidszorg, paramedici, laboratorium
[2] => 4
)
在本例中level 1 -> followingID = 0
Level 2 -> followingID = 1
我不能想出一种方法来做到这一点,而不把一切手动在jquery脚本
我明白了。虽然不漂亮,但很有效。有人知道更好的解决方案吗?
<script>
$(document).ready(function() {
var followingID = 0;
$(".jobs").hide();
$("#0").show();
$(".jobs").change(function() {
var id = $(this).attr("id");
var ref = $(this).attr("value");
$(".jobs").hide();
$(".jobs").removeAttr("name");
$("#0").show();
$("#" + id).show();
$("#" + this.value).show();
$('option[ref="' + id + '"]').parent().show();
if ($("#" + this.value).length == 0) {
$("#" + id).attr("name", "sdata[]");
}
});
});
</script>
<?
function renderAllJobs() {
$jobs = getJobs();
$followingID = -1;
$html = '';
foreach ($jobs as $job) {
if ($followingID != $job[2]) {
if ($followingID > -1) {
$html .= '</select></p>';
}
$followingID = $job[2];
$html .= '<p><select class="jobs" ref="' . $job[2] . '" id="' . $followingID . '">
<option value="0">Maak een keuze</option>';
}
$html .= '<option ref="' . $job[0] . '" value="' . $job[0] . '">' . $job[1] . '</option>';
}
$html .= '</select></p>';
return $html;
}
echo renderAllJobs();