单击时将文本设置为文本框

时间:2016-07-12 08:15:43

标签: javascript jquery asp.net asp.net-mvc

我无法理解,如何在按钮点击时将文本设置为文本框。 这是我的标记:

图片: enter image description here

标记:

 <div id="deploymentsGrid">
    @if (db.CatalogCircumstances.Any())

    {
        @grid.GetHtml(
        tableStyle: "table",
        headerStyle: "table_HeaderStyle",
        footerStyle: "table_PagerStyle",
        rowStyle: "table_RowStyle",
        alternatingRowStyle: "table_AlternatingRowStyle",
        selectedRowStyle: "table_SelectedRowStyle",
        columns: grid.Columns(

            grid.Column("Nr", @Resources.Localization.nr, format: @<text>
                @{ row = row + 1;} @item.Nr
            </text>, style: "p13"),

            grid.Column("Description", @Resources.Localization.description, format: @<text>
                        @{ row = row + 1;} @item.Description
            </text>, style: "width:250px"),


            grid.Column("", "", format: @<text>
                                <input id="select_bttn" style="width:78px" type="submit" value=@Resources.Localization.select />
            </text>)
                )
            )
    }
    <br />
</div>




<div class="container">
    @using (Html.BeginForm("AddCircumstanceToList", "Health", FormMethod.Post))
    {
        @Html.HiddenFor(m => m.EmployeeId)

        @Html.TextBoxFor(m => m.Nr, Model.Nr, new { style = "width:100px" })
        @Html.TextBoxFor(m => m.Description, Model.Description, new { style = "width:450px" })
        <br />
            <br />
            @Html.LabelFor(m => m.Start_date)
            @Html.TextBoxFor(m => m.Start_date, new { id = "datepicker5" })
            @Html.LabelFor(m => m.End_date)
            @Html.TextBoxFor(m => m.End_date, new { id = "datepicker4" })

            <br /><br />
            <input type="submit" value="@Resources.Localization.save" style="width:78px" />
    }
    <button id="closer">@Resources.Localization.back</button>
</div>

我想点击'select-bttn'文本设置为文本框(在beginform内)然后发布到保存按钮单击的操作。

2 个答案:

答案 0 :(得分:1)

首先将select_bttn替换为:

<input id="select_bttn" class="select_bttn" style="width:78px" type="button" value="OK" data-nr="@item.Nr" data-description="@item.Description" />

其次添加一个jQuery函数,它将获取所单击行的NrDescription属性,并设置两个文本框的值:

$(function () {
    $(".select_bttn").click(function () {
        var nr = $(this).data('nr');
        var description = $(this).data('description');

        var message = "You clicked on.Nr - " + nr + ".Description - " + description + ".";
        alert(message);

        $("#Nr").val(nr);
        $("#Description").val(description);
    });
});

<强>输出:

Button click event in MVC WebGrid

答案 1 :(得分:0)

1在表单中添加ID

@using (Html.BeginForm(     
         "AddCircumstanceToList", "Health", FormMethod.Post, 
         new { id = "myForm" }))}

2添加此jquery

$( "#select_bttn" ).click(function() { 
    $("#Description").val("hello world");
    $("#myForm").submit();
});