简体   繁体   English

将jQuery应用于php回显的元素

[英]Applying jquery to elements echoed from php

EDIT: THE PROBLEMS WERE CAUSED BY AN IMPROPERLY LOADING JQUERY LIBRARY, AN ISSUE NOT SPELLED OUT CLEARLY IN MY ORIGINAL QUESTION. 编辑:问题是由加载不正确的JQUERY库引起的,这个问题在我的原始问题中并未明确提出。 APOLOGIES. 道歉。

I'm using jquery to hide, post, then echo the results of a php query to two separate divs. 我使用jquery隐藏,发布,然后将php查询的结果回显到两个单独的div。 I then want to be able to swap the resulting images across these divs. 然后,我希望能够在这些div之间交换结果图像。 The first part works fine, but I'm unable to get any other jquery scripts to work on these divs (eg sortable, droppable, etc). 第一部分工作正常,但我无法在这些div上使用任何其他jquery脚本(例如,sortable,droppable等)。

I'm new to scripting within the past few weeks. 在过去的几周内,我是脚本的新手。 I think I need to json encode the php stuff before I send it, but I'm having trouble finding clear guidance online about how to do this. 我认为我需要先对php内容进行json编码,然后再发送,但是我很难在网上找到有关如何执行此操作的明确指南。 Any help much appreciated, whether either descriptive, referral to specific intro resources (eg not php.net), or with code itself. 任何帮助,无论是描述性的,引荐到特定的介绍性资源(例如,不是php.net)还是代码本身,都应得到赞赏。

I am including relevant scripts below. 我在下面包括相关脚本。 This one works: 这个作品:

<script type='text/javascript'> 
$(document).ready(function(){ 
$("#search_results").slideUp(); 
    $("#search_button").click(function(e){ 
        e.preventDefault(); 
        ajax_search(); 
    }); 
    $("#search_term").keyup(function(e){ 
        e.preventDefault(); 
        ajax_search(); 
    });    
}); 

function ajax_search(){     
  $( "#search_results").show(); 
  var search_val = $("#search_term").val();
  var search_valb = $("#search_theme").val(); 
  $.post( "./find.php", {search_term : search_val, search_theme : search_valb}, function(data){
       if (data.length>0){ 
         $( "#search_results").html(data);  
         $( ".portfolio_container").hide();
         $( ".portfolio_draggables").hide();
         $( "ul.clickable_container li").click(function(){
             $( ".portfolio_draggables").hide();
             var activeImage = $(this).find("a").attr('href'); 
             $( ".portfolio_container").show();
             $( activeImage).show(); 
             return false;           
        });  

       };
   });
};
</script>

This is the html form that I'm using. 这是我正在使用的html表单。

<div id="lettersearchform" class = "lettersearchform">
        <form id="searchform" method="post">          
        <label for="search_term">Enter your word here!</label> 
        <input type="text" name="search_term[]" id="search_term" /> 
        <!--<input type="submit" value="search" id="search_button" />-->
        </form> 
</div>     

<div id="search_results"></div>

The "search_results" are generated successfully with this script: 使用以下脚本成功生成了“ search_results”:

$alpharray = array();
    while($row = mysqli_fetch_assoc($result)){      
        $alpharray[$row['letter']][] = $row;
    }

$first = $second = array();
foreach( str_split( $_POST['search_term']) as $alpha)
{ 
    $first[] = "<li><a href = '#$alpha'><img class='imgs_clickable_droppable' img src='../Letterproject/images/{$alpharray[$alpha][0]['photoPath']}' width='100' height='140'></src></a></li>";
    $editable = array("<div id='$alpha' class='portfolio_draggables'>");
    foreach ($alpharray[$alpha] as $tempvar)
    {                                              
         $editable[] = "<a href ='findall.php'><img src='../Letterproject/images/{$tempvar['photoPath']}' width='70' height='110'></src></a>";                                                         
    }   
    $editable[] = '</div>';
    $second[] = implode( '', $editable);
}

    echo '<ul id = "clickable" class="clickable_container">';
        echo implode( '', $first);
        echo '</ul>';


    echo '<div id="portfolio" class = "portfolio_container">';
        echo implode( '', $second);
    echo '</div>';

So here's where the problem enters: This sortable script is more limited than the cross-div I want, but this type of thing won't work. 所以这里是问题所在:这种可排序的脚本比我想要的跨div更为受限制,但是这种类型的东西行不通。

$(function() {

    $("ul.clickable li").draggable({
        containment: 'parent',
        revert: 'invalid',
        opacity: '0.91',
        cursor: 'crosshair'
    });
    $('.clickable').sortable();
}); 

For those interested in more context, I'm using jquery to post the input from a form. 对于那些对更多上下文感兴趣的人,我正在使用jquery从表单发布输入。 A Php script matches the characters inputted to a corresponding letter image in a mysql db. Php脚本将输入的字符与mysql db中对应的字母图像匹配。 It then echoes a list of these images to one div and echoes the whole portfolio of all images of that letter to another div. 然后,它将这些图像的列表回显到一个div,并将该字母的所有图像的整个组合反射到另一个div。 The idea is that a user could drag images from this second output to replace a letter image in the other div. 这个想法是用户可以从第二个输出中拖动图像来替换另一个div中的字母图像。

Thanks so much! 非常感谢!

$("ul.clickable li") 

does not correctly select an element with an HTML id of clickable and a class of clickable container. 不能正确选择HTML ID为clickable且元素为clickable容器的元素。 Either change the class of the element (which is selected with . in jQuery/CSS) or the id (which is selected with #) 更改元素的类(在jQuery / CSS中用。选择)或id(用#选择)

$("#clickable")

should work. 应该管用。

Additionally, /src is unnecessary as src is not a tag. 另外,/ src是不必要的,因为src不是标签。 Writing out HTML directly in PHP script is not the cleanest way of doing this. 用PHP脚本直接写出HTML并不是最干净的方法。 I highly recommend you check out Head First HTML/CSS or HF HTML5 to augment some of your understanding. 我强烈建议您查看Head First HTML / CSSHF HTML5 ,以加深您的理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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