开发者

Ajax Id Auto Incrementing

开发者 https://www.devze.com 2023-03-25 10:30 出处:网络
I will probably sound or look dumb by this but I need to learn.Check out the following part of a code:

I will probably sound or look dumb by this but I need to learn. Check out the following part of a code:

       $('.buttonclass').click(function(){ 
        var button_id = $(this).attr('id'); 
     $('#'+button_id).click(function(){   

           var开发者_如何学Python yes = $("input#yes").val();
    if (yes == "") {
   return false;
   }
var id = $("input#id").val();
    if (id == "") {
  return false;
}

        var dataString = 'yes='+ yes + '&id=' + id;



            //try insted this    //alert (dataString);return false;  
              $.ajax({  type: "POST",  dataType:'HTML', 
              //or the appropiate type of data you are getting back 
               url: "http://www.edshaer.com/EdinburgCISD/Gorena/Gorena.php",      data: dataString,


                //in the php file do $email = $_POST['email']; 



                  //not a good practice but you can try with it and without it 
                   success: function(data) {  



                $("#div").hide(data).fadeOut();
                   $("#div").html(data);
                   $("#div").show(data).fadeIn();


                   // Change the content of the message element    

                      // Fade the element back in 
                         }    });
                            //ajax ends 
                                         return false; });
                                         //click ends   
                                          });//document ready ends

My button ID that is being submitted in my html page is sending random numbers. For example It can be:

                $("#383").click(function() {                                      

or it can be:

                  $("#521").click(function() { 

My question is, how do I do it to auto increment the ID of the button clicked so that no matter what ID number is clicked it will still run the run the code smoothly... Right now I have this:

       $('.buttonclass').click(function(){ 
      var button_id = $(this).attr('id'); 
          $('#'+button_id).click(function(){   

Hopefully someone can help me... let me know if you need more info... Thank you in advanced...

Here is part of my HTML code... Hopefully it will be a little more understandable...

    <?php
   $data3 = mysql_query("SELECT * FROM `EdinburgCISDGorenamessage` 
   ORDER BY `ID` DESC LIMIT 0, 100") 
       or die(mysql_error()); 
          echo "<div id=\"div\">    <table align=\"center\" width=\"570\">"; 
        while($info3 = mysql_fetch_array( $data3 )) 
          { 

           $id = $info3['ID'];
          }
       ?>

       <form name="contact" id="post" method="post" action="">
         <input  id="id" value="<?php echo $id?>"/>
           <input type="submit" class="buttonclass" id="<?php echo $id?>" name="<?php echo $id?>" value="Yes" />

       <input type="submit" id="no<?php echo $id ?>" name="no<?php echo $id ?>" value=" No " /> </form>

I don' want to provide the whole code because its too messy and it doesn't go with the question... Let me know if you need anything else.


Use a class instead. Add a class to the button, and an incremental id you give it while printing it out in your html (I suppose you echo buttons in a loop?), and then just use one snippet:

$('.buttonclass').click(function(){

   var button_id = $(this).attr('id');
   $('#'+button_id).click(function(){
     // your function here
      alert(button_id); // just to see if ID is retrieved
   )};
});

So, if you have

<button id="325" class="buttonclass" type="button">BUTTON 325</button>
<button id="150" class="buttonclass" type="button">BUTTON 150</button>

The ID of the button you click is retrieved only when you press it

0

精彩评论

暂无评论...
验证码 换一张
取 消