This is a migrated thread and some comments may be shown as answers.

[Solved] Get selected value of dropdownlist using jquery

1 Answer 100 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
MrSamuel
Top achievements
Rank 1
MrSamuel asked on 13 May 2011, 07:02 AM
First of all ... Sorry for my bad English 
Then, i got problem with how to get selected value when i use telerik dropdownlist. I try to search from google but the result doesn't satisfy my expect . Here my code. Please help me 
I 'm using asp.net MVC 2.0 ( This is the first time i use MVC 2.0 )
Here 's my view
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SuperMarket.Models.CImportBillDetail>" %>
<%@ Import Namespace="EditorDemo.Helpers" %>            
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Lập Hóa Đơn Nhập Hàng
</asp:Content>
 
<asp:Content ID="Content2" ContentPlaceHolderID="RightContent" runat="server">
<%= Html.Telerik().StyleSheetRegistrar()
        .DefaultGroup(group => group.Add("telerik.common.css")
                                    .Add("telerik.vista.css"))
%>
 
    <h2>Lập Hóa Đơn Nhập Hàng</h2>   
    <div id="form">
        <div>
            <table cellspacing="5" cellpadding="5" style="padding-left:50px; background:none repeat scroll 0 0 #ECF8FD" width="100%"">
                <tr>
                    <td>Mặt Hàng: </td>
                    <td>   
                        <%= Html.Telerik().DropDownList()
                        .Name("DDLMatHang")
                        .BindTo((IEnumerable<SelectListItem>)ViewData["MatHang"])
                        .HtmlAttributes(new { id = "DDListMatHang" })%>
                    </td>
                </tr>
                <tr>
                    <td>Đơn Vị Tính: </td>
                    <td>   
                        <%= Html.Telerik().DropDownList()
                        .Name("DDLDonViTinh")
                        .BindTo((IEnumerable<SelectListItem>)ViewData["DonViTinh"])
                        .HtmlAttributes(new { id = "DDListDonViTinh" })%>
                    </td>
                </tr>
                <tr>
                    <td>Số Lượng Nhập: </td>
                    <td><%= Html.Telerik().NumericTextBoxFor(i => i.SoLuongNhap).MinValue(0).MaxValue(10000).InputHtmlAttributes(new { id = "numSoLuong" })%></td>
                </tr>
                <tr>
                    <td>Giá Nhập: </td>
                    <td><%= Html.Telerik().NumericTextBoxFor(i => i.GiaNhap).MinValue(0).MaxValue(decimal.MaxValue).IncrementStep(500).InputHtmlAttributes(new { id = "numGiaNhap" })%></td>
                </tr>
                <tr>
                    <td></td>
                    <td> <%= Html.ActionLink("Thêm", "AddItem", null, new { id = "addItem" })%></td>
 
                </tr>
            </table>
        </div>
        </br></br>
        <div class="editorRows">
            <div class="row">
                <div class="cellLeft">Mã</div>
                <div class="cellCenter">Tên Mặt Hàng</div>
                <div class="cellCenter">Đơn Vị Tính</div>
                <div class="cellCenter">Số Lượng Nhập</div>
                <div class="cellCenter">Giá Nhập</div>
                <div class="cellRight">Xóa</div>
            </div>
        </div>
        <div >
            <input type="submit" value="Finished" />
        </div>
    </div>
<script type="text/javascript" src="../../Content/AdminTemplate/jquery-1.3.2.js"></script>
<script type="text/javascript" src="../../Content/AdminTemplate/MicrosoftAjax.js"></script>
<script type="text/javascript" src="../../Content/AdminTemplate/MicrosoftMvcValidation.debug.js"></script>
<script type="text/javascript" src="../../Content/AdminTemplate/listEditor.js"></script>
<%= Html.Telerik().ScriptRegistrar() %>
</asp:Content>


And listEditor.js . I tried to use many ways like

$("#yourdropdownid option:selected").val();
$("#<%=DDListMatHang.ClientID %>").val()
$("#DDListMatHang").val()

$("#addItem").click(function () {
    alert($("#<%=DDListMatHang.ClientID %>").val());
    var maMH = $("#DDListMatHang").val();
    var maDVT = $("#DDListDonViTinh").val();
    var gia = $("#numGiaNhap").val();
    var soLuong = $("#numSoLuong").val();
        $.ajax({
            url: "/ImportBill/AddItem",
            cache: false,
            data: "maMH=" + maMH + "&maDVT=" + maDVT + "&gia=" + gia + "&soLuong=" + soLuong,
            success: function (html) {
                if (html != "Error") {
                    $(".editorRows").append(html);
                    $("#numGiaNhap").val(0);
                    $("#numSoLuong").val(0);
                }
                else
                    alert("Có lỗi khi tạo sản phẩm này");
            }
        });
        return false;
});
 
$("a.deleteRow").live("click", function () {
    var proID = $(this).parent().parent().children(":first").html();
    var linkDel = $(this);
    $.ajax({
        url: "/ImportBill/DeleteItem",
        cache: false,
        data: "ma=" + proID,
        success: function (html) {
            if (html == "1" || html == 1) {
                linkDel.parent().parent().remove();
            }
            else {
                alert("Có lỗi khi xóa sản phẩm này");
            }
        }
    });
    return false;
});

1 Answer, 1 is accepted

Sort by
0
Accepted
freddy
Top achievements
Rank 1
answered on 18 May 2011, 05:54 PM
You are indicating the control to use "DDLMatHang" as ID and name using .Name("DDLMatHang"), but are trying to set other ID by HtmlAttributes ("DDListMatHang").
Just delete the HtmlAttributes and use DDLMatHang to get the control:

var ddlctrl = $('#DDLMatHang').data('tComboBox');
Tags
ComboBox
Asked by
MrSamuel
Top achievements
Rank 1
Answers by
freddy
Top achievements
Rank 1
Share this question
or