简体   繁体   中英

Bind textbox with response from onclick event - jquery

I have a textbox in my view page. when i click on imageIcon,it will take data from db and return in alert successfully. But, when i try to bind this response data to textbox, it is not binded correctly. My code in view page is as following :

@foreach (var dateitem in list)
                    {                        
                            <td id="HoursTxt">
                                @Html.TextAreaFor(modelitem => dateitem.Hours, new { id = string.Format("txtHours"), style = "width:50%;height:70%;" }) 
                                @Html.Hidden("CuDate", dateitem.Date)
                                    <img src="~/Images/comment.png" class="prevtest" />
                                <div style="border:solid;display:none;">                                   
                                    <input type="text" id="TxtNotess" />
                                    <input type="button" id="BtnComment" value="Save" />
                                </div>
                            </td>
             }

In my onclick event of imageIcon jquery is following:

$('.prevtest').on('click', function () {        
        var Cudate = $(this).prev('#CuDate').val();
        var ProjId = parseInt($(this).parents('tr').find('input[type="hidden"]').val());
        var TskId =parseInt($(this).parents('tr').find('td').find('#testTaskId').val());       
        $.ajax({
            type: "POST",
            url: "/Timesheet/GetComments?ProjectId=" + ProjId + "&TaskId= " + TskId + "&date= " + Cudate,
            success : function(data)
            {
                alert(data);
                $('#TxtNotess').val(data);
                alert('success');
            },
            error:function()
            {
                alert('Error');
            }
        });
        $(this).next().toggle();
    });

the text box with id TxtNotess not bind with response value Can anyone help me to do this.. Thanks in advance..

First: ID of an element must be unique so use class for the textfield TxtNotess .

<input type="text" class="TxtNotess" />

Then, in the success handler find the textfild with class TxtNotess inside the next sibling of the clicked element

$('.prevtest').on('click', function () {
    var Cudate = $(this).prev('#CuDate').val();
    var ProjId = parseInt($(this).parents('tr').find('input[type="hidden"]').val());
    var TskId = parseInt($(this).parents('tr').find('td').find('#testTaskId').val());
    $.ajax({
        type: "POST",
        url: "/Timesheet/GetComments?ProjectId=" + ProjId + "&TaskId= " + TskId + "&date= " + Cudate,
        context: this,//pass a custom context to the ajax handlers - here the clicked element reference
        success: function (data) {
            alert(data);
            $(this).next().find('.TxtNotess').val(data);//find the target textfield
            alert('success');
        },
        error: function () {
            alert('Error');
        }
    });
    $(this).next().toggle();
});
$('.prevtest').on('click', function () {
    var Cudate = $(this).prev('#CuDate').val();
    var ProjId = parseInt($(this).parents('tr').find('input[type="hidden"]').val());
    var TskId = parseInt($(this).parents('tr').find('td').find('#testTaskId').val());
    $.ajax({
        type: "POST",
        url: "/Timesheet/GetComments?ProjectId=" + ProjId + "&TaskId= " + TskId + "&date= " + Cudate,
        context: this,//pass a custom context to the ajax handlers - here the clicked element reference
        success: function (data) {
            alert(data);
            $('.TxtNotess').val(data);//find the target textfield
            alert('success');
        },
        error: function () {
            alert('Error');
        }
    });
    $(this).next().toggle();
});

I gets resolved by using class name for textbox..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM