Hi i needed some help where if i select a drop down and select from ajax option and a hidden input field appear how can i do it ?
JavaScript
x
16
16
1
<div class="form-row">
2
<div class="col">
3
<label for="select-price-mode" class="col-form-label">Price Mode</label>
4
<select class="select-price-mode custom-select-sm col-10" id="select-price-mode" required>
5
<option selected disabled value="">Select .</option>
6
</select>
7
8
</div>
9
10
<div class="col" hidden>
11
<label for="select-payment-frequency" class="col-form-label">Payment Frequency</label>
12
<select class="select-payment-frequency custom-select-sm col-10" id="select-payment-frequency" required>
13
<option selected disabled value="">Select .</option>
14
</select>
15
16
</div>
This is my ajax
JavaScript
1
38
38
1
// Here the calling Ajax for the drop down menu below
2
$.ajax({
3
// The url that you're going to post
4
/*
5
6
This is the url that you're going to put to call the
7
backend api,
8
in this case, it's
9
https://ecoexchange.dscloud.me:8080/api/get (production env)
10
11
*/
12
url:"https://ecoexchange.dscloud.me:8090/api/get",
13
// The HTTP method that you're planning to use
14
// i.e. GET, POST, PUT, DELETE
15
// In this case it's a get method, so we'll use GET
16
method:"GET",
17
// In this case, we are going to use headers as
18
headers:{
19
// The query you're planning to call
20
// i.e. <query> can be UserGet(0), RecyclableGet(0), etc.
21
query:"PriceModeGet()",
22
// Gets the apikey from the sessionStorage
23
apikey:sessionStorage.getItem("apikey")
24
},
25
success:function(data,textStatus,xhr) {
26
console.log(data);
27
for (let option of data) {
28
$('#select-price-mode').append($('<option>', {
29
value: option.PriceMode,
30
text: option.PriceMode
31
}));
32
}
33
},
34
error:function(xhr,textStatus,err) {
35
console.log(err);
36
}
37
});
38
and this is my ajax response
JavaScript
1
9
1
[
2
{
3
"PriceMode": "Price By Recyclables"
4
},
5
{
6
"PriceMode": "Service Charger"
7
}
8
]
9
Where say if i select Price By Recyclables the hidden drop down list appear how can i do it ?
Advertisement
Answer
You can use the onchange event to trigger a check and if the user selected the value you want, then display the selectbox. You’d have to add an id to the div with the hidden prop (divToDisplay
).
JavaScript
1
6
1
$("#select-price-mode").change(function() {
2
if(this.value === "Price By Recyclables") {
3
$('#divToDisplay').removeAttr('hidden');
4
}
5
});
6