How to select the default selection on the item in my list box using jquery
Using this code I am inserting the code into listbox.
<select id="lstCodelist" size="17" name="lstCodelist" style="width:100%;height:280px;background-color:#EFEFFB;"></select>
Using this code I am displaying data in to lstCodelist box.
$.fn.fillSelectDD = function (data) {
return this.clearSelectDD().each(function () {
if (this.tagName == 'SELECT') {
var dropdownList = this;
$.each(data, function (index, optionData) {
var option = new Option(optionData.Text, optionData.Value);
if ($.browser.msie) {
dropdownList.add(option);
}
else {
dropdownList.add(option, null);
}
});
}
});
}
This is the function I am calling to insert into one list box to other list.
function DoInsert(ind) {
var sourceIndex = $("#lstAvailableCode").val();
var targetIndex = $("#lstCodelist").val();
var success = 0;
var rightSelectedIndex = $("#lstCodelist").get(0).selectedIndex;
var functionName = "/Ajax/SaveCodeforInsert";
if (ind == "plan") {
functionName = "/Ajax/SaveCodeforInsertForPlan";
}
$.ajax({
type: "POST",
traditional: true,
url: functionName,
async: false,
data: "ControlPlanNum=" + $("#ddControlPlan").val() + "&LevelNum=" + $("#ddlLevel").val() + "&ColumnNum=" + $("#ddlColumn").val() + "&SourcbaObjectID=" + sourceIndex + "&TargetbaObjectID=" + targetIndex + "&userID=<%=Model.userID%>",
dataType: "json",
error: function (data) {
alert("Error Adding Code");
FinishAjaxLoading();
},
success: function (data) {
开发者_运维百科 if (data == 0) { success = 1; } else { success = data; }
// $("#lstCodelist option").eq(1).attr('selected', 'selected')
$("#lstCodelist option:first-child").attr("selected", "selected");
FinishAjaxLoading();
}
});
but using this code in my success function I am not able to select assign or hightlight or select to this lstCodelist box.
// $("#lstCodelist option").eq(1).attr('selected', 'selected')
$("#lstCodelist option:first-child").attr("selected", "selected");
but its not working in my code right now is that I am doing something wrong here?
Thanks
I think in selects, just use $("#select_id").val(default_val);
to select the value you need.
http://api.jquery.com/val/
精彩评论