Всем привет! Скажу сразу я не опытный программист PHP и если что-то не так, прошу не агрится. Есть код с сайта, в нем разметка селект от которого зависит еще 1 селект и вот надо что бы во втором селекте при выборе пункта и перезагрузки страницы сохранялся выбранный пункт. Сайт на WordPress. Подойдет любая помощь, спасибо)
PHP/HTML |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
| <div class="higway_wrap" style="padding: 0 0 10px;">
<select class="form-element" name="filter[direction][]">
<option value="">Направление</option>
<option data-parent="0" value="12">Север</option>
<option data-parent="0" value="59">Северо-Восток</option>
<option data-parent="0" value="13">Восток</option>
<option data-parent="0" value="57">Юго-Восток</option>
<option data-parent="0" value="17">Юг</option>
<option data-parent="0" value="16">Юго-Запад</option>
<option data-parent="0" value="15">Запад</option>
<option data-parent="0" value="14">Северо-Запад</option>
</select>
<select class="form-element highway" name="filter[highway][]">
<option value="">Шоссе</option>
<?php foreach ($highways as $highway) { ?>
<option data-parent="<?= $highway->parent ?>"
value="<?= $highway->term_id ?>"><?= $highway->name ?></option>
<?php
} ?>
</select> <a href="" class="mc_del" onclick="return del_parent(jQuery(this));">Удалить</a>
</div>
<?php $highway_html = trim(preg_replace('/([\r\n][\t\s]*)/', '', ob_get_clean()));
$select_count = 1;
if (!empty($filter['highway'])) {
foreach ($filter['highway'] as $direction_id => $ar_highways) {
$select_count += count($ar_highways);
}
$select_count--;
}
echo str_repeat($highway_html, $select_count) ?>
</div>
<script>
jQuery('.higway_wrap:first .mc_del').remove();
jQuery(function () {
highway_options(jQuery('.highway'));
});
jQuery('[name=filter\\[direction\\]\\[\\]]').not('.bind').addClass('bind').change(function () {
highway_options(jQuery(this).next());
});
jQuery('input.num').bind('input change keyup paste', function (e) {
var cursor_position = this.value.substr(0, this.selectionStart).replace(/[\D]/g, '').length;
this.value = this.value.replace(/[\D]/g, '');
this.selectionStart = this.selectionEnd = cursor_position;
});
<?php
if (!empty(count($filter['highway']))) {
$k = 0;
foreach ($filter['highway'] as $direction_id => $ar_highways) {
foreach ($ar_highways as $highway) { ?>
jQuery('[name=filter\\[direction\\]\\[\\]]').eq(<?= $k ?>).val(<?= $direction_id ?>);
setTimeout(function () {
highway_options(jQuery('[name^=filter\\[highway\\]]').eq(<?= $k ?>));
jQuery('[name^=filter\\[highway\\]]').eq(<?= $k ?>).val(<?= $highway ?>);
}, 500);
<?php
$k++;
}
}
}
?>
function highway_options(obj) {
obj.find('option').hide();
var current_options = obj.attr('name', 'filter[highway][' + (obj.prev().val() || '') + '][]').find('option[data-parent="' + obj.prev().val() + '"]');
obj.val(current_options.show().first().val());
}
function add_highway() {
if (jQuery('.higway_wrap').length < jQuery('[name^=filter\\[highway\\]]:first').find('option').length)
var html = '<?= $highway_html ?>';
jQuery('.higway_wrap:last').after(html);
jQuery('[name=filter\\[direction\\]\\[\\]]').not('.bind').addClass('bind').change(function () {
highway_options(jQuery(this).next());
});
highway_options(jQuery('.highway:last'));
}
function del_parent(obj) {
obj.closest('.higway_wrap').remove();
return false;
}
</script> |
|