开发者

How to read arrays generated by EditorFor using jQuery

开发者 https://www.devze.com 2023-02-11 06:11 出处:网络
I am quite confused with technique that I am using. I am using EditorFor to display values. The code for the values to be generated are as follows:

I am quite confused with technique that I am using. I am using EditorFor to display values.

The code for the values to be generated are as follows:

    <tr>
                <td>@Html.HiddenFor(m => m.ID)@Html.CheckBoxFor(m => m.Authorized)</td>
                <td>@Html.DisplayFor(m => m.ID)</td>
                <td>@Html.DisplayFor(m=>m.UserName) </td>
           </tr>

My aim here is upon the Checkbox is being checked, I need开发者_运维问答 to post the ID value as follows:

 $("input:checkbox").live('click', function () {
            if ($(this).attr('checked')) {

                var ID = $(this).parent().parent().find('#ID').val();

                $.ajax({
                    type: "POST",
                    url: '<%=Url.Action("Edit","Employee") %>',
                    data: JSON.stringify(ID),
                    contentType: "application/json; charset=utf-8",
                    dataType: "html",
                    success: function () {
                    },
                    error: function (request, status, error) {
                        alert("Error: " & request.responseText);
                    }
                });
            }            });

However, var ID = $(this).parent().parent().find('#ID').val(); is undefined. How can I he read the ID value from the following generated HTML:

  <td><input id="Employee_0__ID" name="Employee[0].ID" type="hidden" value="1100" /><input id="Employee_0__Authorized" name="Employee[0].Authorized" type="checkbox" value="true" /><input name="Employee[0].Authorized" type="hidden" value="false" /></td>
<td>user </td>


Not sure I understand what value you looking for?

Would this.

var ID = $(this).parent().parent().find('#ID').attr('value');

or this

var ID = $(this).parent().parent().find('#ID').attr('id');

work?


For the HTML specified this should suffice as the hidden element is the previous sibling of the checkbox in the DOM

var ID = $(this).prev().val();
0

精彩评论

暂无评论...
验证码 换一张
取 消