简体   繁体   English

回发后jQuery无法正常工作

[英]After post back jquery not working

I have combo box in which I fill data from ajax jquery. 我有一个组合框,可以在其中填充来自ajax jquery的数据。 It works fine but when I click save button post back occurs after post back it not works. 它工作正常,但是当我单击“保存”按钮时,回发发生在回发之后,它不起作用。

This is my Jquery code. 这是我的Jquery代码。

 $(document).ready(function () {
 var EmpCombo = $('#Cmb_PEmp');
                        var textbox = $("#TxBx_BasicSalary");
                        var BasicSalary = $('#Hid_BasicSalary');
                        var EmpID = $('#Hid_EmpID');
  EmpCombo.on('change', function EmpCmbFuction(e) {
                    e.preventDefault();
                    myEvent();

                });
function myEvent() {
                    var EmployeeId = $('#Cmb_PEmp :selected').val();
                    if (EmployeeId == 0) {
                        return;
                    }
                    $.ajax({
                        type: "POST",
                        cache: false,
                        contentType: "application/json; charset=utf-8",
                        url: '/WebService/GetBasicSalaryByEmpID.asmx/GetSalaryByEmpId',
                        data: '{ "EmployeeId": "' + EmployeeId + '" }',
                        dataType: 'json',
                        success: function (data) {
                            var data = $.parseJSON(data.d)

                            if (data["BasicSalary"] == 0) {
                                textbox.val("No BasicSalary Define Yet");
                                return;
                            }
                            textbox.val(data["BasicSalary"]);
                            BasicSalary.val(data["BasicSalary"]);
                            EmpID.val(EmployeeId);

                        },
                        error: function () { alert("error"); }
                    });
                }
 var prm = Sys.WebForms.PageRequestManager.getInstance();
                prm.add_endRequest(function () {
                    $(document).ready(function () {
 EmpCombo.on('change', function EmpCmbFuction(e) {
                            e.preventDefault();
                            myEvent();

                        });
});

How I will fix this problem after post back it does not give me proper result? 回发后无法给我正确的结果,我将如何解决此问题? Jquery does not perform any function. jQuery不执行任何功能。

Try using 尝试使用

function pageLoad() 
{

}

Insted of $(document).ready() 插入$(document).ready()

function myEvent() {
    // Put all your code
}
$(document).ready(function () {
    myEvent();
});
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function () {
    myEvent();
});

Please try the below script part: 请尝试以下脚本部分:

         function pageLoad(sender, args) {
         //Your jquery code
         }



     I hope this will work.

As you are using jQuery and you are handling your data using jQuery ajax, why don't you put a separate placeholder (that deosn't use update panel). 在使用jQuery并使用jQuery ajax处理数据时,为什么不放置单独的占位符(该占位符不使用更新面板)。 And then your code will difinitly work eg define a placeholder like this; 然后您的代码将正常工作,例如,定义一个这样的占位符;

<asp:Content ID="Content3" ContentPlaceHolderID="RemoveUpdatePanelPlaceHolder" runat="Server">
   // your content goes here
</asp:Content>

I can see you are already using the PageRequestManager needed in this type of issues, but you need to put all your code, to make it work properly like this: 我可以看到您已经在使用这类问题所需的PageRequestManager ,但是您需要放入所有代码以使其正常工作,如下所示:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function () {
    $(document).ready(function () {
        var EmpCombo = $('#Cmb_PEmp');
        var textbox = $("#TxBx_BasicSalary");
        var BasicSalary = $('#Hid_BasicSalary');
        var EmpID = $('#Hid_EmpID');
        EmpCombo.on('change', function EmpCmbFuction(e) {
            e.preventDefault();
            myEvent();

        });

        function myEvent() {
            // Put all your myEvent function code here

        }
    });
});

document.ready函数仅触发一次,发布后也不会触发。尝试使用page_load()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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