Save value in dropdown using javascript


Save value in dropdown using javascript
I want to set default values in nested dropdowns of Country > State > City > Zone @ the time of form editing.
All data are filled in all dropdowns but the value is not set.
I have used select2 dropdown and filling data using jquery ajax in asp.net form.
=======================================================================
function Edit()
{
selCountryId = $(this).find("intCountryId").text();
$("#select2-selCountry-container").text($(this).find("strCountryName").text());
$('#selCountry').trigger('change');
selStateId = $(this).find("intStateId").text();
$('#selState').trigger('change');
$("#select2-selState-container").text($(this).find("strStateName").text());
selCityId = $(this).find("intCityId").text();
$('#selCity').trigger('change');
$("#select2-selCity-container").text($(this).find("strCityName").text());
}
=======================================================================
function getCountry() {
$.ajax({
type: "POST",
url: "Country.aspx/GetCountry",
data: JSON.stringify({}),
contentType: "application/json; charset=utf-8",
dataType: "JSON",
success: function (response) {
var name = "#selCountry";
var ddl = $(name);
var Col_Key = "intCountryID";
var Col_val = "strCountryName";
ddl.find('option').remove();
$(response.d).find("tblCountry").each(function () {
var OptionValue = $(this).find(Col_Key).text();
var OptionText = $(this).find(Col_val).text();
var option = $("<option>" + OptionText + "</option>");
option.attr("value", OptionValue);
ddl.append(option);
});
$(ddl).val(selCountryId);
},
failure: function (response) {
}
});
}
=======================================================================
function getState() {
$.ajax({
type: "POST",
url: "State.aspx/GetState",
data: JSON.stringify({
COUNTRYID: countryid,
ACTION: Action
}),
contentType: "application/json; charset=utf-8",
dataType: "JSON",
success: function (response) {
var name = "#selState";
var ddl = $(name);
var Col_Key = "intStateID";
var Col_val = "strStateName";
ddl.find('option').remove();
$(response.d).find("tblState").each(function () {
var OptionValue = $(this).find(Col_Key).text();
var OptionText = $(this).find(Col_val).text();
var option = $("<option>" + OptionText + "</option>");
option.attr("value", OptionValue);
ddl.append(option);
});
$(ddl).val(selStateId);
},
failure: function (response) {
}
});
}
=======================================================================
function getCity() {
$.ajax({
type: "POST",
url: "City.aspx/GetCity",
data: JSON.stringify({
STATEID: countryid,
ACTION: Action
}),
contentType: "application/json; charset=utf-8",
dataType: "JSON",
success: function (response) {
var name = "#selCity";
var ddl = $(name);
var Col_Key = "intCityID";
var Col_val = "strCityName";
ddl.find('option').remove();
$(response.d).find("tblCity").each(function () {
var OptionValue = $(this).find(Col_Key).text();
var OptionText = $(this).find(Col_val).text();
var option = $("<option>" + OptionText + "</option>");
option.attr("value", OptionValue);
ddl.append(option);
});
$(ddl).val(selCityId);
},
failure: function (response) {
}
});
}
=======================================================================
find
selCountryId = $(this).find("intCountryId").text();
find
$(this)
Hi Wazz Using this we can command js to get current element ID instead of using object name every time we can use this
– Mrunal Shah
yesterday
1 Answer
1
function getCountry()
$(ddl).val('0'); // instead of $(ddl).val(selCountryId);
function getState()
$(ddl).val('0'); // instead of $(ddl).val(selStateId);
function getCity()
$(ddl).val('0'); // instead of $(ddl).val(selCityId);
function Edit()
{
var intddlCountry = '0', strddlCountry = '0', intddlState = '0', strddlState = '0', intddlCity = '0', strddlCity = '0';
intddlCountry = $(this).find("intCountryId").text();
strddlCountry = $(this).find("strCountryName").text();
intddlState = $(this).find("intStateID").text();
strddlState = $(this).find("intState").text();
intddlCity = $(this).find("intCityID").text();
strddlCity = $(this).find("strCityName").text();
$.ajax({
url: getCountry(),
async: true,
success: function (response) {
$("#selCountry").val(intddlCountry);
$("#select2-selCountry-container").text(strddlCountry);
$.ajax({
url: GetState(),
async: true,
success: function (response) {
$("#selState").val(intddlState);
$("#select2-selState-container").text(strddlState);
$.ajax({
url: getCity(),
success: function (response) {
$("#selCity").val(intddlCity);
$("#select2-selCity-container").text(strddlCity);
}
});
}
});
}
});
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Does this
find
work:selCountryId = $(this).find("intCountryId").text();
.? I'm pretty new tofind
but it's supposed to take an element or jQuery object. And the title of the post says 'Save'. Should it say 'Set'? And what is$(this)
?– wazz
Jul 20 at 0:15