Pass a list view value to ajax

I have a list that is being created dynamically. The part of the code that creates the list is:

<ul>
    <?
        foreach ($folders as $folder)
            {
                $folder = str_replace("{imap.gmail.com:993/imap/ssl}", "", $folder);
                $folder2 = str_replace("[Gmail]/", "", $folder);
            ?>
                <li>
                    <a><div id="box"><? echo $folder2; ?> </div></a>
                </li>
            <?}
</ul>

<div id="maillist"></div>

o / p from $ folder

GMAIL/All Mail
GMAIL/Drafts
GMAIL/Important

o / p $ folder2

All Mail
Drafts
Important

what I want is that when the user clicks on "All mail", the corresponding value (in this case: GMAIL / All Mail) should go to another script via ajax. The same process should follow other values ​​in the list as well

ajax code

<script>
$(document).ready(function(){
    $('#box').change(function(){

        var boxid = $('#box').val();
        console.log($('#box'))
        if(boxid != 0)
        {

            $.ajax({
                type:'post',
                url:'a_fetchmaillist.php',
                data:{id:boxid},
                cache:false,
                success: function(returndata){
                    $('#maillist').html(returndata);
                    console.log(returndata)
                }
            });
        }
    })
})
</script>   

Can anyone say if I can do what I want, and if so, how it will be done

+4
source share
1 answer

, (, box).

(, box-allMails) :

foreach ($folders as $folder)
{
   $folder = str_replace("{imap.gmail.com:993/imap/ssl}", "", $folder);
   $folder2 = str_replace("[Gmail]/", "", $folder);
?>
   <li>
     <div class="box" data-folder="<? echo $folder2 ?>"><? echo $folder2; ?></div>
   </li>
<?}

:

$(document).on('click', '.box', function() {
   var folder = $(this).attr('data-folder');
   // ajax call..
});

UPDATE

: 'click' "change", div, ( ).

: :

$(document).on('click', '.dynamic-element', function() { .. })

:

$('.element').on('click', function() { .. });

, .

Clickable. , , . .box div, , :

CSS

.box { cursor:pointer }

jQuery (. )

$(document).on('click', '.box', function() {
  var folder = $(this).attr('data-folder');
  alert('You clicked on: ' + folder);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li><div class="box" data-folder="folderOne">Folder One</div></li>
  <li><div class="box" data-folder="folderTwo">Folder Two</div></li>
  <li><div class="box" data-folder="folderThree">Folder Three</div></li>
  <li><div class="box" data-folder="folderFour">Folder Four</div></li>
</ul>
+3

Source: https://habr.com/ru/post/1605686/


All Articles