0

I have a page where on button click checkboxe's have to selected and discount value has to be entered. this value goes to another php page via ajax call to discount_table where the Id and discount value gets inserted into database and the new content is looded into the same page in named "content". after that if i again select the checkbox and post the data via ajax the click function does not works. Can anyone please tell me where my code is going wrong!!

<script>
   $(document).ready(function () { 
     $('#click').on("click",function(){
       var arrCheckboxes = document.myform1.elements["dataString[]"];
       var checkCount = 0;
       for (var i = 0; i < arrCheckboxes.length; i++) {
       checkCount += (arrCheckboxes[i].checked) ? 1 : 0;
       }
       if(notEmpty(document.getElementById('discount_value'), 'Please Enter a Value')==true)
      {
        if (checkCount > 0)
        {
            var ans= confirm("Are you sure to want to add Discounted Value for the selected checkboxes?");
            if(ans)
            {
               //alert(confirm);
               favfunct();
            }
            else
            {
                return false;
            }
       } 
       else 
       {
          alert("You Have not selected any Checkbox!!");
          return false;
        }
         }
     }); 
  });

   function favfunct() 
   {

    var dataString= $('#myform1 input[type=checkbox]:checked').serializeArray();
    var arr = Array();
    var j=0;
    for(i=0;i<dataString.length;i++)
    {
        arr[j++]= dataString[i].value;
    }
    var discount = document.getElementById('discount_value').value;
    typeName = jQuery("select[name='cust_type']").val();
    jQuery.ajax({
            type: "POST",
            url: "discount_table.php",
            data:"&discount="+discount+"&typeName="+typeName+"&dataString="+arr,
            success: function(response){
            $('.content').html(response);
            }
        });
     }
   </script>

 This is my script in head section.
 I am new to php So please avoid if any mistakes.
 Here is my html code:
    <div class="content">   
    <form action="#" method="post" enctype="multipart/form-data"  style="width:990px;" id="myform1" name="myform1">
    <h2 style="font-style: italic;float: left;color:#8B3A3A;font-size: 30;font-weight: bold; position: relative; left: -40px;">Discount to be Added</h2><br /><br />
     <?php echo '<div><table style="position:relative;right:-75px; "cellpadding="10"cellspacing="10">'; 
     echo "<th style='padding-right:45px;padding-bottom:10px;color:#8B3A3A;'>Sr.No</th><th style='color:#8B3A3A;padding-right:45px;padding-bottom:10px;'> Category Name</th><th style='color:#8B3A3A;padding-right:45px;padding-bottom:10px;'> Sub Category Name</th>";

            $i=0;
            while(@$up = mysql_fetch_array($result))
            {  

                echo "<tr> 
                      <td> ";?>
                         <input id="CheckBoxID[]" type="checkbox" class="ids" name="dataString[]" value="<?php if($up['CatID']=="--"){echo $up['SubCatID'].',1';}else{echo $up['CatID'].',0';};?>"/>
          </td>
                      <td style='padding-right:30px'>
                                <input name="catname[]" type="text" id="name" value="<?php echo $up['CatNm'];?>" readonly=""style="border: none;"/>
          </td>
                      <td style='padding-right:30px'>
                                <input name="subcatname[]" type="text" id="name" value="<?php echo $up['SubCatNm'];?>"readonly="" style="border: none;"/>
                      </td>
             <?php $i++; }
              echo "</table></div>";}?><br />
              <div style="position: relative;left:100px;"> <label for="discount">Enter the Discounted value:</label>
              <input type="text"  id="discount_value" name="discount_value" required="required"/><br /><br />
         <input type="button" value="Add Discount" name="add_discount" onclick="" id="click" style="width:100px;text-align:center;padding: 3px;"/></div>
         </form>
         <?php     
              $result1=mysql_query("Select c.CatNm,'--' AS SubCatNm,Discount_PER from discount d,category c where CustTypeID=$typeID and d.CatID=c.CatID and d.SubCatID is NULL UNION Select c.CatNm,s.SubCatNm,Discount_PER from discount d,subcategory s,category c where CustTypeID=$typeID and d.SubCatID=s.SubCatID and s.CatID=c.CatID");
      $cnt1=1;
      $rows1=mysql_num_rows($result1);
      if($rows1>0)
      {?>
      <div> 
         <form action="#" method="post" enctype="multipart/form-data"  style="width:990px;">
          <h2 style="font-style: italic;float: left;color:#8B3A3A;font-size: 30;font-weight: bold; position: relative; left: -40px;">Category/SubCategory With Discount</h2><br /><br />
         <?php echo '<div style=""><table style="position:relative;right:-75px; "cellpadding="10"cellspacing="10">'; 
         echo "<th style='padding-right:45px;padding-bottom:10px;color:#8B3A3A;'>Sr.No</th><th style='color:#8B3A3A;padding-right:45px;padding-bottom:10px;'> Category Name</th><th style='color:#8B3A3A;padding-right:45px;padding-bottom:10px;'> Sub Category Name</th><th style='color:#8B3A3A;padding-right:45px;padding-bottom:10px;'>Discount</th>";
         while(@$up = mysql_fetch_array($result1))
         {  
                //$pid=$up['SubCatID'];
                echo "<tr> 
             <td> 
            ".$cnt1."
             </td>
                         <td style='padding-right:30px'> 
            ".$up['CatNm']."
             </td>";

                                echo "<td style='padding-right:30px'>
                                ".$up['SubCatNm']."
                                </td>";    



            echo "<td style='padding-right:20px; padding-bottom:10px;'>
                               ".$up['Discount_PER']."
                            </td>
                    </tr>";
                    $cnt1++;
            }
         echo "</table></div>";}?>
         </div>
         </div>
4

1 回答 1

0

您需要将事件处理程序附加到动态附加的元素。使用jQuery,该.on()方法将事件处理程序附加到 jQuery 对象中当前选定的元素集。从 开始jQuery 1.7,该.on()方法提供了附加事件处理程序所需的所有功能。

从您的脚本中可以清楚地看出您正在使用该.on()方法。所以这是我的建议

删除$(document).ready()并将您的$('#click').on("click",function(){})代码放入以下函数后

$(document).on('click','#click',function(){

});

所以它必须看起来像:

$(document).on('click','#click',function(){
    var arrCheckboxes = document.myform1.elements["dataString[]"];
    var checkCount = 0;
    for (var i = 0; i < arrCheckboxes.length; i++) {
        checkCount += (arrCheckboxes[i].checked) ? 1 : 0;
    }

    if(notEmpty(document.getElementById('discount_value'), 'Please Enter a Value')==true) {
        if (checkCount > 0) {
            var ans= confirm("Are you sure to want to add Discounted Value for the selected checkboxes?");
            if(ans) {
               //alert(confirm);
               favfunct();
            } else {
                return false;
            }
        } else {
          alert("You Have not selected any Checkbox!!");
          return false;
        }
    }
});
于 2013-04-29T12:57:35.633 回答