Im been having a trouble how can I get the the value of my select option, I been using javascript to set the value of select option, below is my code which is returning the province value to number instead of text. The problem is how can I convert the number to text, is there any expert who share solution about this.
views.py
JavaScript
x
6
1
def sample_data(request):
2
if request.method=='POST':
3
province = request.POST['list1']
4
print(province) #return ex. 2 depend on select value
5
return render (request,'sample.html')
6
Select option – my reference link
JavaScript
1
4
1
<select id="list1" name ="list1" onchange="updateList('list2', this);"></select>
2
<select id="list2" name ="list2" onchange="updateList('list3', this);"></select>>
3
<select id="list3" name ="list3"></select>
4
javascript
JavaScript
1
83
83
1
let data = [{"id":1,"name":"USA","parentid":0},
2
{"id":2,"name":"Japan","parentid":0},
3
{"id":3,"name":"Europe","parentid":0},
4
{"id":4,"name":"California","parentid":1},
5
{"id":5,"name":"Oklahoma","parentid":1},
6
{"id":6,"name":"Arizona","parentid":1},
7
{"id":7,"name":"Kantô","parentid":2},
8
{"id":8,"name":"Kansai","parentid":2},
9
{"id":9,"name":"Chügoku","parentid":2},
10
{"id":10,"name":"France","parentid":3},
11
{"id":11,"name":"Deutschland","parentid":3},
12
{"id":12,"name":"Espana","parentid":3},
13
{"id":13,"name":"Sacramento","parentid":4},
14
{"id":14,"name":"Los Angeles","parentid":4},
15
{"id":15,"name":"San Diego","parentid":4},
16
{"id":16,"name":"Tulsa","parentid":5},
17
{"id":17,"name":"Oklahoma City","parentid":5},
18
{"id":18,"name":"Lawton","parentid":5},
19
{"id":19,"name":"Phoenix","parentid":6},
20
{"id":20,"name":"Flagstaff","parentid":6},
21
{"id":21,"name":"Tucson","parentid":6},
22
{"id":21,"name":"Tokyo","parentid":7},
23
{"id":22,"name":"Chiba","parentid":7},
24
{"id":23,"name":"Tochigi","parentid":7},
25
{"id":24,"name":"Kyoto","parentid":8},
26
{"id":25,"name":"Osaka","parentid":8},
27
{"id":26,"name":"Nara","parentid":8},
28
{"id":27,"name":"Tottori","parentid":9},
29
{"id":28,"name":"Hirochima","parentid":9},
30
{"id":29,"name":"Okayama","parentid":9},
31
{"id":30,"name":"Quimper","parentid":10},
32
{"id":31,"name":"Toulouse","parentid":10},
33
{"id":32,"name":"Nancy","parentid":10},
34
{"id":33,"name":"Dusseldorf","parentid":11},
35
{"id":34,"name":"Leipzig","parentid":11},
36
{"id":35,"name":"Munchen","parentid":11},
37
{"id":36,"name":"Barcelona","parentid":12},
38
{"id":37,"name":"Sevilla","parentid":12},
39
{"id":38,"name":"Guernica","parentid":12}]
40
41
function populateList(list, pid) {
42
let l = document.getElementById(list);
43
l.innerHTML = "";
44
let topItem = document.createElement("option");
45
topItem.value = 0;
46
topItem.text = "--Select--";
47
l.appendChild(topItem);
48
let items = data.filter(item => item.parentid == pid);
49
items.forEach(function(item){
50
let newItem = document.createElement("option");
51
newItem.value = item.id;
52
newItem.text = item.name;
53
l.appendChild(newItem);
54
})
55
}
56
57
function updateList(selList, thisList) {
58
if (thisList.value != 0) {
59
populateList(selList, Number(thisList.value));
60
} else {
61
let s = document.getElementById(selList);
62
s.value = 0;
63
triggerEvent(s, "onchange");
64
let sCopy = s.cloneNode(false);
65
let p = s.parentNode;
66
p.replaceChild(sCopy, s);
67
}
68
}
69
function triggerEvent(e, trigger)
70
{
71
if ((e[trigger] || false) && typeof e[trigger] == 'function')
72
{
73
e[trigger](e);
74
}
75
}
76
77
78
function loadList1() {
79
populateList("list1", 0);
80
}
81
82
window.onload = loadList1;
83
Advertisement
Answer
You need to pass name
value here newItem.value
because on submit the option value
attribute get passed to backend. Then after changing that still you need id
to populate your next select-box so you can create custom attribute with value as id
and then pass same to your function.
Demo code :
JavaScript
1
243
243
1
let data = [{
2
"id": 1,
3
"name": "USA",
4
"parentid": 0
5
},
6
{
7
"id": 2,
8
"name": "Japan",
9
"parentid": 0
10
},
11
{
12
"id": 3,
13
"name": "Europe",
14
"parentid": 0
15
},
16
{
17
"id": 4,
18
"name": "California",
19
"parentid": 1
20
},
21
{
22
"id": 5,
23
"name": "Oklahoma",
24
"parentid": 1
25
},
26
{
27
"id": 6,
28
"name": "Arizona",
29
"parentid": 1
30
},
31
{
32
"id": 7,
33
"name": "Kantô",
34
"parentid": 2
35
},
36
{
37
"id": 8,
38
"name": "Kansai",
39
"parentid": 2
40
},
41
{
42
"id": 9,
43
"name": "Chügoku",
44
"parentid": 2
45
},
46
{
47
"id": 10,
48
"name": "France",
49
"parentid": 3
50
},
51
{
52
"id": 11,
53
"name": "Deutschland",
54
"parentid": 3
55
},
56
{
57
"id": 12,
58
"name": "Espana",
59
"parentid": 3
60
},
61
{
62
"id": 13,
63
"name": "Sacramento",
64
"parentid": 4
65
},
66
{
67
"id": 14,
68
"name": "Los Angeles",
69
"parentid": 4
70
},
71
{
72
"id": 15,
73
"name": "San Diego",
74
"parentid": 4
75
},
76
{
77
"id": 16,
78
"name": "Tulsa",
79
"parentid": 5
80
},
81
{
82
"id": 17,
83
"name": "Oklahoma City",
84
"parentid": 5
85
},
86
{
87
"id": 18,
88
"name": "Lawton",
89
"parentid": 5
90
},
91
{
92
"id": 19,
93
"name": "Phoenix",
94
"parentid": 6
95
},
96
{
97
"id": 20,
98
"name": "Flagstaff",
99
"parentid": 6
100
},
101
{
102
"id": 21,
103
"name": "Tucson",
104
"parentid": 6
105
},
106
{
107
"id": 21,
108
"name": "Tokyo",
109
"parentid": 7
110
},
111
{
112
"id": 22,
113
"name": "Chiba",
114
"parentid": 7
115
},
116
{
117
"id": 23,
118
"name": "Tochigi",
119
"parentid": 7
120
},
121
{
122
"id": 24,
123
"name": "Kyoto",
124
"parentid": 8
125
},
126
{
127
"id": 25,
128
"name": "Osaka",
129
"parentid": 8
130
},
131
{
132
"id": 26,
133
"name": "Nara",
134
"parentid": 8
135
},
136
{
137
"id": 27,
138
"name": "Tottori",
139
"parentid": 9
140
},
141
{
142
"id": 28,
143
"name": "Hirochima",
144
"parentid": 9
145
},
146
{
147
"id": 29,
148
"name": "Okayama",
149
"parentid": 9
150
},
151
{
152
"id": 30,
153
"name": "Quimper",
154
"parentid": 10
155
},
156
{
157
"id": 31,
158
"name": "Toulouse",
159
"parentid": 10
160
},
161
{
162
"id": 32,
163
"name": "Nancy",
164
"parentid": 10
165
},
166
{
167
"id": 33,
168
"name": "Dusseldorf",
169
"parentid": 11
170
},
171
{
172
"id": 34,
173
"name": "Leipzig",
174
"parentid": 11
175
},
176
{
177
"id": 35,
178
"name": "Munchen",
179
"parentid": 11
180
},
181
{
182
"id": 36,
183
"name": "Barcelona",
184
"parentid": 12
185
},
186
{
187
"id": 37,
188
"name": "Sevilla",
189
"parentid": 12
190
},
191
{
192
"id": 38,
193
"name": "Guernica",
194
"parentid": 12
195
}
196
]
197
198
function populateList(list, pid) {
199
200
let l = document.getElementById(list);
201
l.innerHTML = "";
202
let topItem = document.createElement("option");
203
topItem.value = 0;
204
topItem.text = "--Select--";
205
l.appendChild(topItem);
206
let items = data.filter(item => item.parentid == pid);
207
items.forEach(function(item) {
208
let newItem = document.createElement("option");
209
newItem.value = item.name;//give name value as well in value
210
newItem.text = item.name;
211
var data_id = document.createAttribute("data-id")//crete custom attribute
212
data_id.value = item.id//set id value there
213
newItem.setAttributeNode(data_id);//pass same to your option
214
l.appendChild(newItem);
215
})
216
}
217
218
function updateList(selList, thisList) {
219
if (thisList.value != 0) {
220
populateList(selList, Number(thisList.options[thisList.selectedIndex].getAttribute('data-id')));//get selectedoption data-id value
221
} else {
222
let s = document.getElementById(selList);
223
s.value = 0;
224
225
triggerEvent(s, "onchange");
226
let sCopy = s.cloneNode(false);
227
let p = s.parentNode;
228
p.replaceChild(sCopy, s);
229
}
230
}
231
232
function triggerEvent(e, trigger) {
233
if ((e[trigger] || false) && typeof e[trigger] == 'function') {
234
e[trigger](e);
235
}
236
}
237
238
239
function loadList1() {
240
populateList("list1", 0);
241
}
242
243
window.onload = loadList1;
JavaScript
1
3
1
<select id="list1" name="list1" onchange="updateList('list2', this);"></select>
2
<select id="list2" name="list2" onchange="updateList('list3', this);"></select>>
3
<select id="list3" name="list3"></select>