如何在mvc中获取复选框值?

时间:2014-02-23 04:52:17

标签: c# javascript jquery asp.net-mvc-4

当我选择复选框并单击时,选择按钮如何将此值传递给文本框值。复选框id = chk和textbox id = OrderNo。请帮助我....我在下面有一些东西......

$("#OrderNo").blur(function() {
    $.get('/Ordering/OrderList/', function(data) {
        $('#orlist').html(data);
    });
    $('#orlist').dialog({
        width: 500,
        height: 350,
        open: true,
        title: 'Select Order',
        buttons: {
            Select: function() {
                if (("#chk") == 'checked') {
                    var por = ("#porderno").val();
                    por = ("#OrderNo");
                }
            },
            Cancel: function() {
                $('#orlist').dialog('close');
                $('#orlist').empty();
            }
        }
    });

部分视图页面是

@model IEnumerable<testcon.OrderM>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.OdrId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.OrderNo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CId)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.CheckBox("OdrId", new { @id="chk"})
        </td>
         <td class="left">
            @Html.DisplayFor(modelItem => item.OrderNo, new { @id="porderno"})
        </td>
        <td class="left">
            @Html.DisplayFor(modelItem => item.CId)
        </td>

    </tr>
}

</table>

我的创建视图页面是

@model testcon.DeliveryInfo

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>DeliveryInfo</legend>

        @*<div class="editor-label">
            @Html.LabelFor(model => model.DId)
        </div>*@
        <div class="editor-field">
           Delivery Id : @Html.EditorFor(model => model.DId)
            @Html.ValidationMessageFor(model => model.DId)
        </div>

        @*<div class="editor-label">
            @Html.LabelFor(model => model.OrderNo)
        </div>*@
        <div class="editor-field">
          Order No :@Html.EditorFor(model => model.OrderNo)
            @Html.ValidationMessageFor(model => model.OrderNo)
            @*<a href="#" onclick="javascript:getOrderList()">Show Order</a>*@
        </div>

       @* <div class="editor-label">
            @Html.LabelFor(model => model.DDate)
        </div>*@
        <div class="editor-field">
           Delivery Date : @Html.EditorFor(model => model.DDate)
            @Html.ValidationMessageFor(model => model.DDate)
        </div>

        @*<div class="editor-label">
            @Html.LabelFor(model => model.DQuantity)
        </div>*@
        <div class="editor-field">
           Delivery Quantity: @Html.EditorFor(model => model.DQuantity)
            @Html.ValidationMessageFor(model => model.DQuantity)
        </div>

        @*<div class="editor-label">
            @Html.LabelFor(model => model.DAmount)
        </div>*@
        <div class="editor-field">
           Delivery Amount : @Html.EditorFor(model => model.DAmount)
            @Html.ValidationMessageFor(model => model.DAmount)
        </div>
        <div id="orlist" >
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

2 个答案:

答案 0 :(得分:2)

使用.is(':checked')检查是否已选中...

buttons: {
    Select: function() {
        if ($("#chk").is(':checked')) {
            $("#OrderNo").val($("#porderno").val());
        }
    },

答案 1 :(得分:0)

检查jQuery中是否选中了复选框,您可以通过

执行此操作
//will return true if the check box is checked otherwise false!
$("#chkBoxId").is(':checked');   

这种方法在处理单选按钮/复选框时非常好。

//will yield the same result if checkbox is checked
$('input[name="chkBoxName"]:checked').val();    

所以你可以在你的代码中使用它们,比如@ Anthony Chu响应了我要写的内容:)