两次点击后,只有jquery为ajax post方法触发

时间:2011-12-27 11:06:51

标签: c# jquery jquery-post

我正在使用ajax post方法将值从aspx发送到php.in,我在数据库中插入值后将按钮单击事件中的ajax方法作为函数调用我将调用脚本但是当我单击时第二次只有那个脚本在第一次点击时调用如何修复它?而且这个方法在firefox中不起作用,但在第二次点击时在IE中工作。

    <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);
}

2 个答案:

答案 0 :(得分:1)

尝试:

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

希望有所帮助

答案 1 :(得分:0)

  1. 您可以在on中使用jquery事件处理程序。 jquery 1.7中不推荐使用live, bind and delegate
  2. 您无需使用attr('value')代替val()即可。
  3. 在ajax调用中加入datatype = 'html'表示response将在html

  4. 此外,您已将$(document).ready(function(){保留在函数resetFields中。这是不需要的。

  5. resetFields元素移除功能button

  6. 然后尝试使用下面的代码,我希望它会帮助你

    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);