如何在javascript中传递文本框值来编辑函数

时间:2017-07-20 13:08:29

标签: javascript c# jquery html css

为什么在onclick javacript函数时不调用文本框值。

 $(document).ready(function () {
     $("input#btnEditExperience").click(function () {
     //$(".errorMsgTitle").show();
     //$(".errorEditTitle").show();
     var id = $(this).attr("name"); 
     var hospitalname = jQuery('[id$=txtHospitalName]').val();
     var department = jQuery('[id$=txtDepartment]').val();
     var designation = jQuery('[id$=txtDesignation]').val();
     var Fromdate = jQuery('[id$=txtFromDate]').val();
     var Todate = jQuery('[id$=txtToDate]').val();
     var Workdescription = jQuery('[id$=txtWorkDescription]').val();
     var Statusval = $("#<%=hdnExperienceID.ClientID%>").val();
     //if (Statusval == '') {
         //    //$('.errorEditTitle').html('Status required!');
         //    Statusval.className += ' errors-text';
         //    return false;
     //}
     //$("div.preloader").show();
         $.ajax({
             type: 'POST',
             url: pageUrl + "/UpdateExperience",
             data: '{ExperienceID: "' + id + '",HospitalName:"' + hospitalname + '",Department:"' + department + '",Designation:"' + designation + '",FromDate:"' + Fromdate + '",ToDate:"' + Todate + '",WorkDescription: "' + Workdescription + '"  }',
             contentType: "application/json; charset=utf-8",
             dataType: 'json',
             success: OndocumentSuccess,
             failure: function (response) {
                 alert(response.d);
             }
         });
     });
});

function OndocumentSuccess(response) {
    //$("div.preloader").fadeOut();
    //$("#backgroundPopup").fadeOut("normal");

    $("#<%=hdnExperienceID.ClientID%>").val('')
    location.reload();
}
$(document).ready(function () {
    $("a.Editview").click(function () {
        $("#<%=hdnExperienceID.ClientID%>").val('');

    });
});
function Editvalue(Edit) {
    $("#<%=hdnExperienceID.ClientID%>").val(Edit.value);
}

**See this Error Image**

<a id="<%#Eval("ExperienceID")%>" data-toggle="modal" data-target="#Experienceedit<%#Eval("ExperienceID")%>" href="" class="edit-me Editview"><i class="tg-edit fa fa-pencil"></i></a> 

ASP:

<div class="modal fade bs-example-modal-lg" id='Experienceedit<%#Eval("ExperienceID")%>' tabindex="-1"
    role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="background-color:black;">
    <div class="modal-header" style="background-color:white;">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
        <h4 id="myModalLabel">Edit Experience</h4>
    </div>
    <div class="modal-body" style="background-color:white;">
        <div class="errorEditTitle"></div>
        <table class="index-table">
            <tr>
                <td>
                    <label>Hospital Name<sub>*</sub></label>
                </td>
                <td>
                    <asp:TextBox ID="txtHospitalName" runat="server" class="span11" MaxLength="100" onchange="javascript:Editvalue(this);" Text='<%#Eval("HospitalName")%>'></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Department<sub>*</sub></label>
                </td>
                <td>
                    <asp:TextBox ID="txtDepartment" runat="server" class="span5" onchange="javascript:Editvalue(this);" Text='<%#Eval("Department")%>'></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Designation<sub>*</sub></label>
                </td>
                <td>
                    <asp:TextBox ID="txtDesignation" runat="server" class="span5" onchange="javascript:Editvalue(this);" Text='<%#Eval("Designation")%>'></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <label>From Date<sub>*</sub></label>
                </td>
                <td>
                    <asp:TextBox ID="txtFromDate" runat="server" Class="form-control" onchange="javascript:Editvalue(this);" Text='<%#Eval("FromDate")%>'></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <label>To Date<sub>*</sub></label>
                </td>
                <td>
                    <asp:TextBox ID="txtToDate" runat="server" Class="form-control" onchange="javascript:Editvalue(this);" Text='<%#Eval("ToDate")%>'></asp:TextBox>
                </td>
            </tr>
             <tr>
                <td>
                    <label>
                        Work Description<sub>*</sub></label>
                </td>
                <td>
                    <asp:TextBox ID="txtWorkDescription" TextMode="MultiLine" runat="server" Class="form-control" onchange="javascript:Editvalue(this);" Text='<%#Eval("WorkDescription")%>'></asp:TextBox>
                </td>
            </tr>
        </table>
    </div>
    <div class="modal-footer" style="background-color:white;">
        <input type="button" class="btn btn-success" id="btnEditExperience"
            value="Update" name="<%#Eval("ExperienceID")%>" />
        <button class="btn btn-danger" data-dismiss="modal" aria-hidden="true">
            Cancel</button>
    </div>
</div>

CodeBehind文件:

[WebMethod]
    public static UserAjax UpdateExperience(string ExperienceID, string HospitalName, string Department, string Designation, string FromDate, string ToDate, string WorkDescription)
    {

        UserAjax oUserAjax = new UserAjax();
        BD_Doctor oDoctor = new BD_Doctor();
        //oDoctor.HospitalName = Experience.Trim();
        oDoctor.HospitalName = HospitalName.Trim();
        oDoctor.Department = Department.Trim();
        oDoctor.Designation = Designation.Trim();
        oDoctor.FromDate = Convert.ToDateTime(FromDate.Trim().ToString());
        oDoctor.ToDate = Convert.ToDateTime(ToDate.Trim().ToString());
        oDoctor.WorkDescription = WorkDescription.Trim();
        oDoctor.ExperienceID = Convert.ToInt32(ExperienceID);
        oDoctor.SaveUpdateInformation();
        return oUserAjax;

    }
    #endregion

1 个答案:

答案 0 :(得分:1)

您可以尝试使用JSON.stringify。还要确保该URL有效。

$("input#btnEditExperience").click(function () {
    var data = {
        hospitalname: $('#<%= txtHospitalName.ClientID %>').val(),
        department: $('#<%= txtDesignation.ClientID %>').val(), 
        ....
    };
    $.ajax({
        type: 'POST',
        url: '<%= ResolveUrl("~/Default.aspx/UpdateExperience") %>',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: OndocumentSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });
});