2011-12-27 22 views
0

射擊我使用AJAX POST方法從ASPX發送值php.in那我打電話阿賈克斯一種方法,在按鈕點擊事件函數中插入值之後數據庫只有我會調用腳本,但當我點擊第二次只有該腳本調用如何解決它,而在第一次本身點擊?也是這種方法是不是在Firefox中工作,但在IE中工作時,點擊第二時間。兩次後單擊僅jQuery是對Ajax的POST方法

<script type="text/javascript" language="javascript"> 
    function resetFields() 
    { 
    $(document).ready(function(){ 
    $("#<%=this.btnAdd.ClientID %>").click(function() { 
    // we want to store the values from the form input box, then send via ajax below 
    var ddlCompany = $("#<%=this.ddlCompany.ClientID %>").attr('value'); 
    var txtLocation = $("#<%=this.txtLocation.ClientID %>").attr('value'); 
    var txtDept = $("#<%=this.txtDept.ClientID %>").attr('value'); 
    var ddlIndustryType = $("#<%=this.ddlIndustryType.ClientID %>").attr('value'); 
    var txtDesg = $("#<%=this.txtDesg.ClientID %>").attr('value'); 
    var ddlFnalArea = $("#<%=this.ddlFnalArea.ClientID %>").attr('value'); 
    var txtExperience = $("#<%=this.txtExperience.ClientID %>").attr('value'); 
    var txtJobDesc = $("#<%=this.txtJobDesc.ClientID %>").attr('value'); 
    var txtEducation = $("#<%=this.txtEducation.ClientID %>").attr('value'); 
    var txtDesiredProfile = $("#<%=this.txtDesiredProfile.ClientID %>").attr('value'); 
    var txtPositionWanted = $("#<%=this.txtPositionWanted.ClientID %>").attr('value'); 
    var txtAddedBy = $("#<%=this.txtAddedBy.ClientID %>").attr('value'); 
    var txtContactName = $("#<%=this.txtContactName.ClientID %>").attr('value'); 
    var txtEmailid = $("#<%=this.txtEmailid.ClientID %>").attr('value'); 
    var txtContactno = $("#<%=this.txtContactno.ClientID %>").attr('value'); 
     $.ajax({ 
     type: "POST", 
     url: "http://172.16.126.32/Riyas/marggroup.com/get-current-openings.php", 
     data: "ddlCompany=" + ddlCompany + "& txtLocation="+ txtLocation+"& txtDept="+ txtDept+"& ddlIndustryType="+ ddlIndustryType+"& txtDesg="+ txtDesg+"& ddlFnalArea=" + ddlFnalArea+"& txtExperience="+ txtExperience+"& txtJobDesc="+ txtJobDesc+"& txtEducation="+ txtEducation+"& txtDesiredProfile="+ txtDesiredProfile+"& txtPositionWanted="+ txtPositionWanted+"& txtAddedBy="+ txtAddedBy+"& txtContactName="+ txtContactName+"& txtEmailid="+ txtEmailid+"& txtContactno="+ txtContactno, 
     success: function(response){ 
     $('div.success').html(response); 
     } 
    }); 
     return false; 
    }); 
    }); 
    } 
    </script> 

代碼隱藏:

protected void btnAdd_Click(object sender, EventArgs e) 
    { 
c.MyQuery("insert into tblHrims_currentOpeningsNew(nvrDesignation,nvrCompany,nvrExperience,nvrLocation,nvrEducation,nvrDepartment,nvrIndustryType,nvrFnalArea,nvrJobDesc,nvrDesiredProfile,nvrContactPerson," + 
" nvrContactNumber,nvrEmailId,nvrWantedPositions,nvrAddedBy,dttAddedon) values('" + txtDesg.Text.Trim().Replace("'", "") + "','" + ddlCompany.SelectedItem.Text + "','" + txtExperience.Text + "','" + txtLocation.Text + "','" + txtEducation.Text + "'," + 
" '" + txtDept.Text.Trim().Replace("'", "") + "','" + ddlIndustryType.SelectedItem.Text + "','" + ddlFnalArea.SelectedItem.Text + "','" + txtJobDesc.Text.Replace("'", "''") + "','" + txtDesiredProfile.Text.Replace("'", "") + "'," + 
" '" + txtContactName.Text.Trim().Replace("'", "") + "','" + txtContactno.Text.Trim().Replace("'", "") + "','" + txtEmailid.Text.Trim().Replace("'", "") + "','" + txtPositionWanted.Text.Trim().Replace("'", "") + "'," + 
" '" + txtAddedBy.Text.Trim().Replace("'", "") + "','" + c.GetValue("select getdate()") + "')"); 
     string strID = c.GetValue("select max(intsno) from tblhrims_currentopeningsNew"); 
Page.ClientScript.RegisterStartupScript(this.GetType(), "reset", " resetFields();", true); 
} 
+0

什麼是resetFields功能,當你打了嗎?還有,爲什麼你把$(文件)。就緒(函數(){}代碼resetFields函數內? – Pavan 2011-12-27 11:15:25

+0

resetFields()函數,我在服務器端函數調用 – 2011-12-27 11:28:52

+0

在另一方面,如果輸入的是一個形式,你可以放在這行:var datastring = $(「#myForm」)。serialize();'。然後你不必手動處理所有東西:) – Flater 2011-12-27 13:11:35

回答

0
  1. 您可以在jquery使用on事件處理程序。 jQuery 1.7中已棄用live, bind and delegate
  2. 你不需要使用attr('value')代替val()會工作。
  3. 包括datatype = 'html'在ajax調用來表示response將在html

  4. 此外,您還保留$(document).ready(function(){函數內resetFields。這是不需要的。

  5. remove函數從button元件resetFields呼叫。

然後嘗試使用下面的代碼,我希望它會幫助你

String scriptString = "<script type=\"text/javascript\">$(document).ready(function(){ 

    $('#<%=this.btnAdd.ClientID %>').on('click',function(){ 
    var ddlCompany = $('#<%=this.ddlCompany.ClientID %>').val(); 
    var txtLocation = $('#<%=this.txtLocation.ClientID %>').val(); 
    var txtDept = $('#<%=this.txtDept.ClientID %>').val(); 
    var ddlIndustryType = $('#<%=this.ddlIndustryType.ClientID %>').val(); 
    var txtDesg = $('#<%=this.txtDesg.ClientID %>').val(); 
    var ddlFnalArea = $('#<%=this.ddlFnalArea.ClientID %>').val(); 
    var txtExperience = $('#<%=this.txtExperience.ClientID %>').val(); 
    var txtJobDesc = $('#<%=this.txtJobDesc.ClientID %>').val(); 
    var txtEducation = $('#<%=this.txtEducation.ClientID %>').val(); 
    var txtDesiredProfile = $('#<%=this.txtDesiredProfile.ClientID %>').val(); 
    var txtPositionWanted = $('#<%=this.txtPositionWanted.ClientID %>')val(); 
    var txtAddedBy = $('#<%=this.txtAddedBy.ClientID %>').val(); 
    var txtContactName = $('#<%=this.txtContactName.ClientID %>').val(); 
    var txtEmailid = $('#<%=this.txtEmailid.ClientID %>').val(); 
    var txtContactno = $('#<%=this.txtContactno.ClientID %>').val(); 

    $.ajax({ 
     type: 'POST', 
     url: 'http://172.16.126.32/Riyas/marggroup.com/get-current-openings.php', 
     data: 'ddlCompany='+ddlCompany+'&txtLocation='+txtLocation+'&txtDept='+txtDept+'&ddlIndustryType='+ddlIndustryType+'&txtDesg='+txtDesg+'&ddlFnalArea='+ddlFnalArea+'&txtExperience='+txtExperience+'&txtJobDesc='+txtJobDesc+'&txtEducation='+txtEducation+'&txtDesiredProfile='+txtDesiredProfile+'&txtPositionWanted='+txtPositionWanted+'&txtAddedBy='+txtAddedBy+'&txtContactName='+txtContactName+'&txtEmailid='+txtEmailid+'&txtContactno='+txtContactno, 
     datatype = 'html', 
     success: function(response){ 
     $('div.success').html(response); 
     } 
     }); 
    }); 
});<s/cript>"; 

Page.ClientScript.RegisterStartupScript(this.GetType(), "reset", scriptString, true); 
+0

您可以編輯我的jQuery,請 – 2011-12-27 11:28:04

+0

也應該在firfox – 2011-12-27 11:31:04

+0

工作好的男人..讓我嘗試..給我一些時間.. – 2011-12-27 11:31:47

1

嘗試:

$("#&lt;%=this.btnAdd.ClientID %>").live('click', function() { 

希望它可以幫助

+1

「live」已被棄用在jquery 1.7中。它沒有建議使用它。 – 2011-12-27 11:21:38

+0

在服務器端我應該怎麼做? – 2011-12-27 11:36:21

+0

你不必對於服務器端改變什麼,只是處理後的數據 – 2011-12-27 11:38:58