我有两个下拉列表,即ddlCountry和ddlState。我正在使用jquery clone()方法来创建ddlCountry和ddlState下拉列表的动态下拉列表控件。
这是我的代码:
<body>
<form id="form1" runat="server">
<asp:Panel ID="ddlPanel" runat="server">
<div>
<asp:Button ID="btnClone" Text="Clone" runat="server" />
</div>
<br />
<br />
<table>
<tr>
<td>Cateogry:
</td>
<td>
<div>
<asp:DropDownList ID="ddlCountryList" runat="server" class="ddlCountryClass"></asp:DropDownList>
</div>
</td>
<td>SubCategory:
</td>
<td>
<div>
<asp:DropDownList ID="ddlStateList" runat="server" class="ddlStateClass"></asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<div id="target">
</div>
</td>
<td>
<div id="target2">
</div>
</td>
</tr>
</table>
<asp:Button ID="btnGet" runat="server" Text="Get Values" OnClick="GetDropDownListValues" />
</asp:Panel>
</form>
<script type="text/javascript">
$(function () {
$("[id*=btnClone]").bind("click", function () {
var index = $("#target select").length + 1;
//Clone the DropDownList
var ddl = $("[id$=ddlCountryList]").clone(true);
//Set the ID and Name
ddl.attr("id", "ddlCountryList_" + index);
ddl.attr("name", "ddlCountryList_" + index);
ddl.append('<option selected="selected" value="0">Select Country</option>');
//[OPTIONAL] Copy the selected value
var selectedValue = $("[id$=ddlCountryList] option:selected").val();
ddl.find("option[value = '" + selectedValue + "']").attr("selected", "selected");
//Append to the DIV.
$("#target").append(ddl);
$("#target").append("<br /><br />");
return false;
});
});
$(function () {
$("[id*=btnClone]").bind("click", function () {
var index = $("#target2 select").length + 1;
var ddl = $("[id$=ddlStateList]").clone();
ddl.attr("id", "ddlStateList_" + index);
ddl.attr("name", "ddlStateList_" + index);
var selectedValue = $("[id$=ddlStateList] option:selected").val();
ddl.find("option[value = '" + selectedValue + "']").attr("selected", "selected");
$("#target2").append(ddl);
$("#target2").append("<br /><br />");
return false;
});
});
// Make Ajax call to fetch the state values.
$(function () {
$('#ddlStateList').attr('disabled', 'disabled');
$('#ddlStateList').attr('disabled', 'disabled');
$('#ddlStateList').append('<option selected="selected" value="0">Select State</option>');
$('#ddlCountryList').change(function () {
var $countryDropdown = $(this); // "this" is the event source
var country = $countryDropdown.val();
// Figure out the index of the country dropdown
var index = $countryDropdown.attr('id').split("_")[1] || "";
if (index) {
index = "_" + index;
}
var $stateDropdown = $("#ddlStateList" + index);
$stateDropdown.removeAttr("disabled");
$.ajax({
type: "POST",
url: "Default.aspx/BindStates",
data: "{'country':'" + country + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var j = jQuery.parseJSON(msg.d);
var options;
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'
}
$stateDropdown.html(options)
},
error: function (data) {
alert('Something Went Wrong')
}
});
});
});
</script>
我想将所选的每个值存储在国家/地区下拉列表(不是ddlstate)中,例如ddlCountry,ddlcountry_1,ddlcountry_2等....在数组中。
我怎样才能实现它?
答案 0 :(得分:0)
for(i=0; i < somecounter; i++){
string ddlCountry = "ddlcountry_" + i;
DropDownList ddl = (DropDownList)ddlPanel.FindControl(ddlCountry);
string value = ddl.SelectedValue.ToString();
}
你必须放一些计数器,每次添加新的下拉列表时,将计数器加1。然后,您将获得动态创建的下拉列表的数量