1

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>
learning
  • 11,415
  • 35
  • 87
  • 154

2 Answers2

0

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?

Craigt
  • 3,418
  • 6
  • 40
  • 56
0

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();
David Glenn
  • 24,412
  • 19
  • 74
  • 94