添加新选项后选择标记选项刷新,并在退出功能之前在javascript中获取新选项附加值

时间:2015-12-24 08:07:21

标签: javascript jquery html

function Edit(id) {
    $.ajax({
        method: 'POST',
        url: "@Url.Action("
        GetThisFile ", "
        Admin ")",
        dataType: "json",
        data: {
            'id': id
        },
        cache: false,
        success: function(viewModel) {
            $("#Id").val(viewModel.TheFile.Id);
            $("#UTitle").val(viewModel.TheFile.Title);
            $("#USummary").val(viewModel.TheFile.Summary);
            $("#UKeyWords").val(viewModel.TheFile.Keyword);
            $("#UDescription").val(viewModel.TheFile.Description);
            $("#UPrice").val(viewModel.TheFile.Price);
            $("#UFileSize").val(viewModel.TheFile.FileSize);
            $("#imgEdit").attr("src", viewModel.TheFile.ImageUrl);
            $("#srcimg").val(viewModel.TheFile.ImageUrl);
            $('#fileEdit').val(viewModel.TheFile.FileUrl);
            $('#LinkDownload').attr('href', viewModel.TheFile
                .FileUrl);
            $('#msg').text('');
            $("#UGroup option").each(function() {
                if ($(this).val() == viewModel.TheFile
                    .SubGroup.Group.GroupId) $(this)
                    .attr('selected', 'selected')
            });
            SubGroupLoad(viewModel.TheFile.SubGroup.Group.GroupId);
            $("#USubGroup option").each(function() {
                if ($(this).val() == viewModel.TheFile
                    .SubGroup.SubGroupId) $(this).attr(
                    'selected', 'selected')
            });
        },
        error: function() {
            alert("error");
        }
    });
}

function SubGroupLoad(id) {
    $.ajax({
        method: 'POST',
        url: '@Url.Action("SubGroupLoad", "Admin")',
        dataType: 'json',
        data: {
            'id': id
        },
        cache: false,
        success: function(viewmodel) {
            $('#USubGroup').empty();
            var len = viewmodel.length;
            var content = '';
            for (var i = 0; i < len; i++) {
                content = '<option Value=';
                content += viewmodel[i].SubGroupId;
                content += '>';
                content += viewmodel[i].SubGroupName;
                content += '</option>';
                $('#USubGroup').append(content);
            }
        },
        error: function() {
            alert("خطا");
        }
    });
}

我想在此代码中使用新选项获取$("#USubGroup option")值:

$("#USubGroup option").each(function() {
    if ($(this).val() == viewModel.TheFile.SubGroup.SubGroupId) 
        $(this).attr('selected', 'selected')
});

0 个答案:

没有答案