I am trying to construct the url parameters for the from the value selected from the input elements.
for example: i have a page where there is a one text input field and 3 select fields(name,department,brand,model),first i will enter the value to name text field,then i will select the value from department dropdown,then i wont select anything from brand dropdown, then i will select some value from model dropdown, finally when i press submit, the values which have been input by user should be put in to object with its key pair and generate the url params with $.params(obj) and append that to ajax call. how can i do it. i have the code below of what i have done.
ex:
var obj = {}
//after click on submit
obj = {
_pick:pick,
_dept:dept,
_model:model // this obj contains no brand object, because i have not selected anything from the brand dropdown
}
html:
<form name="populateForm">
<div class="dom_element">
<label>Department</label>
<input type="text" name="pick" id="pick">
</div>
<div class="dom_element">
<label>Department</label>
<select id="departmentSelect">
<option value="0">Select Department</option>
<option value="122">Department-1</option>
<option value="123">Department-2</option>
<option value="124">Department-3</option>
<option value="125">Department-4</option>
<option value="126">Department-5</option>
</select>
</div>
<div class="dom_element">
<label>Brands</label>
<select id="brandSelect">
<option value="0">Select Brand</option>
<option value="212">Brand-1</option>
<option value="213">Brand-2</option>
<option value="214">Brand-3</option>
<option value="215">Brand-4</option>
<option value="216">Brand-5</option>
</select>
</div>
<div class="dom_element">
<label>Model</label>
<select id="modelSelect">
<option value="0">Select Model</option>
<option value="328">Model-1</option>
<option value="324">Model-2</option>
<option value="326">Model-3</option>
<option value="325">Model-4</option>
<option value="322">Model-5</option>
</select>
</div>
</form>
<button class="ApplyFilter">Apply</button>
js:
$(document).ready(function(){
$('.ApplyFilter').on('click',function(){
var paramObj = {};
var pick = $('#pick').val();
var dept = $('#departmentSelect').val();
var brand = $('#brandSelect').val();
var model = $('#modelSelect').val();
if(pick != ''){
paramObj = {
_pick:pick
}
}
if(dept > 0){
paramObj = {
_dept:dept
}
}
if(brand > 0){
paramObj = {
_brand:brand
}
}
if(model > 0){
paramObj = {
_model:model
}
}
/*
this is the object i am expecting at the end, if any input does not have any value then that should not be in this object
paramObj = {
_pick:pick,
_dept:dept,
_brand:brand,
_model:model
}
*/
var urlParams = $.params(paramObj);
if(urlParams.length > 0){
$.ajax({
type: "POST",
url: "/api/getMydata/?"+urlParams,
success:function(response){
console.log(response.responseData);
}
})
}
});
});
=to set the parameters because you're assigning the variableparamObjto a new object that has only one parameter. Use something likeparamObj._paramName = ....paramnotparams.