简体   繁体   中英

Get value from dynamic href using jquery ajax php

I am new to Ajax , Jquery & PHP . I am stuck in code I hope someone can guide me to resolve the issue:

   <script type="text/javascript">
      $(document).ready(function(){
      $('#number_filters a').bind("click", function() {
    var year  = $(this).attr('href');
    $.post('getdata.php',{resultyear:year},function(res){
        $("#showresult").html(res);
    });
                return false;
  });

      });
    </script>

 Following code, if anyone click on link below, I am send a value by POST method to a php page and the returned data are displayed in DIV Tag:

<div id="number_filters">
<a href="1" class="c1">Link 1</a>
<a href="2" class="c1">Link 2</a>
</div>

 this is the div tag I use to display results from above code:

 <div id="showresult"></div>

 Up until now it has been working fine, and below is the output which I get from dynamic page:

 Output: 2015 2014 2013 2012 2011

Now my question is, if any one click on 2015 or 2014 I want to show again dynamic data retrieved database.

Can anyone give me some idea or code I can try to resolve the issue please?

Modify the output in you php file that generates the output like:

$output = "<a href='2015'>2015</a> <a href='2014'>2014</a>";

And update your jquery with this code:

$('#showresult').on('click', 'a', function(){
    var year = $(this).attr('href');

    // Ajax Request
    $.post(....);
});

.on() helps you to achieve that you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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