使用jquery在下拉列表中选择一个项目

时间:2014-06-11 09:41:56

标签: c# jquery asp.net select drop-down-menu

我正在使用以下代码来使用jquery

绑定下拉列表
if ($("#status_input option[value='" + status + "']").length > 0) {

    $('#status_input').val(status);
}
else {

    $('#status_input').get(0).selectedIndex = -1;
}

在状态我得到“1”,我没有得到任何错误,但无法绑定dropdownliast以及这停止工作代码,意味着当我评论此代码重置信息绑定成功,但是,当我正在使用此代码,它不会抛出任何错误,也不会绑定结果。

请帮我解决这个问题。

UPDATE ::

我的HTML是::

<asp:Repeater ID="OffersRepeater" runat="server">
<ItemTemplate>
    <div class="grid_row">
        <div class="offer_id" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "OfferId") %>
        </div>
        <div class="offer_active" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "ActiveStatus")%>
        </div>
        <div class="offer_partner" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "Partner") %>
        </div>
        <div class="offer_description" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "Description") %>
        </div>
        <div class="offer_image" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "ImageSource") %>
        </div>
        <div class="offer_url" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "Url") %>
        </div>
        <div class="offer_url_text" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "UrlText") %>
        </div>
        <div class="offer_email" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "Email") %>
        </div>
        <div class="offer_phone" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "Phone") %>
        </div>
        <div class="offer_status" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "IsActive").ToString().ToLower()=="true" ? "1" : "0" %>
        </div>
        <div class="offer_approval_date" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "ApprovalDate") is DateTime ? ((DateTime)DataBinder.Eval(Container.DataItem, "ApprovalDate")).ToString( "MM-dd-yyyy") : "" %>
        </div>
        <div class="offer_start_date" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "StartDate") is DateTime ? ((DateTime)DataBinder.Eval(Container.DataItem, "StartDate")).ToString( "MM-dd-yyyy") : ""%>
        </div>
        <div class="offer_end_date" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "EndDate") is DateTime ? ((DateTime)DataBinder.Eval(Container.DataItem, "EndDate")).ToString( "MM-dd-yyyy") : ""%>
        </div>
        <div class="offer_legal" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "LegalInformation") %>
        </div>
        <div class="offer_twitter" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "Twitter") %>
        </div>
        <div class="offer_facebook" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "Facebook")%>
        </div>
        <div class="offer_legalonofferpage" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "LegalOnOfferPage")%>
        </div>
        <div class="offer_Season" style="display: none;">
            <%# DataBinder.Eval(Container.DataItem, "Season")%>
        </div>
        <div class="grid_row_background" runat="server" id="RowBackground">
        </div>
        <div class="grid_row_foreground">
            <div class="grid_cell" style="padding-left: 10px; width: 420px;">
                <span class='<%# DataBinder.Eval(Container.DataItem, "ActiveStatus")%>'></span>
                <%# DataBinder.Eval(Container.DataItem, "Partner") %>
            </div>
            <div class="grid_cell">
                <a href="javascript:void(0)" onclick="ViewXmlOffer(this);" title="View Offer" class="icon_button view_icon">
                </a>
                <a href="javascript:void(0)" onclick="editOffer(this);" title="Edit Offer" class="icon_button edit_icon">
                </a>
                <a href="javascript:void(0)" onclick="deleteOffer(this);" title="Delete Offer" class="icon_button delete_icon"></a>
            </div>
            <div class="clear">
            </div>
        </div>
    </div>
</ItemTemplate>
</asp:Repeater>

这是DRPDOWN

<div class="box_foreground">
<div class="form_field_text" title="OFFER STATUS" style="width: 298px;">
    OFFER STATUS</div>
<select id="status_input" class="form_input" style="width: 308px;">
    <option value="1" selected="selected">ACTIVE</option>
    <option value="0">DEACTIVATED</option>
</select>
</div>

我的JQUERY CODE是::

function editOffer(link) {

$('#offerEntryBox').data('offer', $(link).parents('.grid_row'));
$('#offerEntryBox').data('category', $(link).parents('.grid').parent());
alert('hi');
resetForm();

$('#offerListBox').fadeOut('fast', function () {
    $('#offerEntryBox').fadeIn('fast'); doResize();
 });
}

我遇到问题的函数是:: resetForm();

function resetForm() {

var partner = '', categoryID = '', url = '', urlText = '', email = '', phone = '', startDate = '', endDate = '', description = '', legal = '', image = '', twitter = '', facebook = '', status = '1', Season='';

var myOfferEditor = $find(offereditorname);
var myLegalEditor = $find(legaleditorname);
$('#Legendbox').css('display', 'none');
var offer = $('#offerEntryBox').data('offer');
if (offer != null && offer != undefined) {
    $('#offerEntryBox').find('.form_title').text('EDIT OFFER');

    partner = $(offer).children('.offer_partner').text();
    url = $(offer).children('.offer_url').text();
    urlText = $(offer).children('.offer_url_text').text();
    email = $(offer).children('.offer_email').text();
    phone = $(offer).children('.offer_phone').text();
    startDate = $(offer).children('.offer_start_date').text();
    endDate = $(offer).children('.offer_end_date').text();
    description = $(offer).children('.offer_description').html();
    legal = $(offer).children('.offer_legal').html();
    image = $(offer).children('.offer_image').text();
    status = $(offer).children('.offer_status').text();
    twitter = $(offer).children('.offer_twitter').text();
    facebook = $(offer).children('.offer_facebook').text();
    Season = $(offer).children('.offer_Season').text();
}
else
    $('#offerEntryBox').find('.form_title').text('NEW OFFER');

var category = $('#offerEntryBox').data('category');
if (category != null && category != undefined)
    categoryID = $(category).children('.category_id').text();

$('#partner_input').val(partner);
$('#url_input').val(url);
$('#url_text_input').val(urlText);
$('#partner_email_input').val(email);
$('#phone_input').val(phone);
myOfferEditor.set_html(description);
myLegalEditor.set_html(legal);
$('#twitter_input').val(twitter);
$('#facebook_input').val(facebook);

 alert(status);
//    if ($('[id$=category_input option[value=22]').length > 0) {
//        alert(categoryID);
//    }
//    if ($("#category_input option[value='" + categoryID + "']").length > 0) {
//        $('#category_input').val(categoryID);
//        alert('entered 1');
//    }
//    else {
//        $('#category_input').get(0).selectedIndex = -1;
//        alert('entered 2');
//    }

if ($("#status_input option[value='" + status + "']").length > 0) {

    $('#status_input').val(status);
}
else {

    $('#status_input').get(0).selectedIndex = -1;
}
$('#start_date_input').val(startDate);

$('#end_date_input').val(endDate);
validateDate($('#start_date_input'));
validateDate($('#end_date_input'));

$('.form_input', '#submit_offer_form').not('select, #start_date_input, #end_date_input').each(function () {
    $(this).siblings('.form_field_text').text($.trim(this.value) == '' ? $(this).siblings('.form_field_text').attr('title') : this.value);
});
$('select.form_input', '#submit_offer_form').each(function () {
    $(this).css('display', 'none');
    $(this).siblings('.form_field_text').text($.trim(this.value) == '' ? $(this).siblings('.form_field_text').attr('title') : $(this).children('option:selected').text())
});


$('#image_preview_container').children('img').remove();
if (image != '' && contentPath != undefined) {
    $('#image_source_input').val(image);
    loadPreview(contentPath, image, $('#image_preview_container'), 195, 98);
}


setBoxHighlight($('.form_field', '#submit_offer_form').add($('#image_source_input').parents('.form_part')), false);
$('.form_status_message').text('');
 setFormInteraction(true);
}

1 个答案:

答案 0 :(得分:0)

我已经解决了问题,实际上该值包含空格。这就是为什么它没有得到确切的价值。我只是修剪字符串值来解决问题。