使用Jquery更改asp:DropDownList的默认选择值
Change asp:DropDownList default selected value with Jquery
我有一个asp:DropDownList控件在一个隐藏的弹出窗口,这个弹出窗口是激活当用户点击一个图标(图像)在Gridview控件的一行
然后我使用一些jquery来选择被点击的行,然后我提取gridview行上的标签控件的值,然后要填充弹出框字段(文本框控件和下拉列表控件默认值),这个想法是使用它们来更新数据库中行中的记录。
我遇到的问题是在弹出框上的下拉控件中填充默认选择。我可以在文本框中填充文本框,而不是下拉框。
下面是其中一个文本框的标记和来自gridview的ddl,我的值来源于此:
<asp:TemplateField HeaderText="Current Stage">
<ItemTemplate>
<asp:Label ID="lblCurrentStage" CssClass="clCurrentStage" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CurrentStage")%>' ToolTip ='<%# DataBinder.Eval(Container.DataItem, "CurrentStage")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Review Date">
<ItemTemplate>
<asp:Label ID="lblReviewDate" CssClass="clReviewDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ReviewDate")%>' ToolTip ='<%# DataBinder.Eval(Container.DataItem, "ReviewDate")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
,下面的代码在文本框上工作得很好,但在ddl上不行:
<div id="PopUpTrackerEditFieldCurrentStage">
<div class="clEditFieldCurrentStageContainer">
<asp:DropDownList ID="ddlPopUpEditCurrentStage" runat="server"> </asp:DropDownList>
</div>
</div>
<div id="PopUpTrackerEditFieldReviewDate">
<div class="clEditFieldReviewDateContainer">
<asp:TextBox ID="tbPopUpEditReviewDate" CssClass="clPopUpDateFieldsInEdit" runat="server" Text="" ToolTip =""></asp:TextBox>
</div>
</div>
下面是用来填充文本框和下拉列表的jquery:
//Store the current row being edited
var row = $(this).closest("tr");
//Get the existing Comments into a string
var strCurrentStage = $(".clCurrentStage", row).text();
//Add the any existing Comments
$("#<%=ddlPopUpEditCurrentStage.ClientID%>").val(strCurrentStage);
//Dynamically add the text to the tooltip
$("#<%=ddlPopUpEditCurrentStage.ClientID%>").attr('title', 'Click to select the current stage here for ' + strPSTNNum);
//Get the existing Review Date into a string
var strReviewDate = $(".clReviewDate", row).text();
//Add the any existing Review Date
$("#<%=tbPopUpEditReviewDate.ClientID%>").val(strReviewDate);
//Dynamically add the text to the tooltip
$("#<%=tbPopUpEditReviewDate.ClientID%>").attr('title', 'Edit the review date here for ' + strPSTNNum);
我知道strCurrentStage是ok的,因为我暂时使用它来填充文本框,看看它是否包含gridview中当前阶段标签的当前阶段文本,它确实包含了。所以我认为问题是,我不能选择下拉列表控件的正确部分来填充默认值。
尝试不使用选择器。例如,您试图将选定值设置为selected:
//Store the current row being edited
var row = $(this).closest("tr");
//Get the existing Comments into a string from the label in the gridview
var strCurrentStage = $(".clCurrentStage", row).text();
//Add the current stage selected value to the ddl control
$("#<%=ddlPopUpCurrentStage.ClientID%>").val(strCurrentStage);
为了其他人的利益,我最终设法对其进行排序的唯一方法是创建一个函数,该函数关闭并从数据库中获得选择,然后在填充部分的部分下方,然后在完成之前将代码添加到该函数的底部。我怀疑的原因是,当OP代码试图添加下拉菜单并放入默认值时,DOM已经看到了变化,我通过使用jquery"Live('mousover',blaa)"来确认这一点。,然后它就工作了。无论如何,下面是代码:
function getListOfStages() {
var ddlCurrentStages = $("#<%=ddlPopUpEditCurrentStage.ClientID%>");
$.ajax({
type: "POST",
url: "PSTN_OrderManagementTracker.aspx/PopCurrentStagesddl",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var CurrentStagesReturned = xml.find("Table");
//for each current stage from the database, add it to the dropdown list on the modal
$.each(CurrentStagesReturned, function (index, CurrentStagesReturned) {
dbCurrentStageName = $(this).find("CurrentStage").text()
ddlCurrentStages.append('<option>' + dbCurrentStageName + '</option>');
//Get the this records existing current stage into a string
var strCurrentStage = $(".clCurrentStage", row).text();
//Add the existing current stage for that record as the default one showing
$("select#<%=ddlPopUpEditCurrentStage.ClientID%>").val(strCurrentStage);
});
},
failure: function (msg) {
alert(msg);
}
});
}
$ (' # & lt; % = ddlPopUpCurrentStage。ClientID %> option:selected').val(strCurrentStage)
- CKeditor:更改对话框中的默认选择选项
- jquery-select2显示默认选择的第一个值
- Shopify多选择器上的默认选择
- Angularjs select:如何获取所选项目的位置并设置默认选择
- 我想使用 javascript 将第一个链接保留为默认选择
- 下拉菜单默认选择从数据库值中选择
- 更改整个文档或窗口的默认选择行为
- 如何使用 jQuery 将下拉选项更改为默认选择
- 默认选择按钮 extjs4.1.
- ng-model 防止选中=“选中”以默认选择单选按钮
- 如何将默认选择的值获取到javascript中,并在页面加载时将其传递给php
- 默认选择剑道组合框
- html默认选择选项 - 不同的颜色 - 不适用于当前的jquery
- 关闭浏览器选项卡时,如何在确认框中默认选择“取消”
- 如何在选择框或下拉框中默认选择第一个选项
- 如何停止jquery中的默认选择事件操作
- angularjs选择框基于数据库id的默认选择
- 选择使用jQuery默认选择一个选项
- Jquery contextMenu插件无法接受默认选择
- 剑道下拉默认选择