简体   繁体   中英

Onclick event not working with AJAX

I want to display selected image's slideshow with the help of ajax and to do this fetching of title is very important to display corresponding slideshow but in JavaScript, title of the clicked image is not fetching.

javascript:

function slide(s) {
    var _event = s;
    alert(_event);
}

code:

<div id="inner_body">
    <?php
    $c = mysql_connect("localhost", "abc", "xyz");
    mysql_select_db("root");
    $sql = "select * from images where year=2000";
    $qc = mysql_query($sql);
    $count = 0;
    while ($ans = mysql_fetch_array($qc)) {
        $title = ucwords($ans['event']);
        print " 
            <div class='img-wrap' onclick='slide($title)'>
                <img id='display_img' src='images/thumbnails/$ans[image1]'>
                <div class='img-overlay'>
                    <b1>" . $title . "</b1>
                </div>
            </div>";
    }
    ?>
</div>

missing quotes tag

onclick='slide($title)'; //render onclick='slide(xxxx)'
//should be
onclick='slide(\"$title\")'; //render onclick='slide("xxxx")'

PS.

image tag must be closed <image /> or <image></image>

<b1> is undefined(also <b></b> is outdated legacy HTML). this should be <strong>...</strong>

pass this on onclick and using jquery find child element with class title get the value of the html.

Javascript

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
    function  slide(s)
    {
        alert($(s).find('.title').html());   
    }
</script>

code

    <div id="inner_body">
    <?php
    $c = mysql_connect("localhost", "abc", "xyz");
    mysql_select_db("root");
    $sql = "select * from images where year=2000";
    $qc = mysql_query($sql);
    $count = 0;
    while ($ans = mysql_fetch_array($qc)) {
        $title = ucwords($ans['event']);
        print " 
            <div class='img-wrap' onclick='slide(this)'>
            <img id='display_img' src='images/thumbnails/$ans[image1]'>
            <div class='img-overlay'>
             <b1 class='title'>" . $title . "</b1>
             </div>
            </div>";
    }
    ?>
</div>

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