【问题标题】:accordion not working after load加载后手风琴不工作
【发布时间】:2014-03-27 07:21:30
【问题描述】:

我刚刚遇到了 jquery 手风琴的问题。我正在做的是从 php 页面“jobsload.php”加载新内容。用新内容更新页面后,手风琴不起作用。我也尝试过destroy属性,但没有成功。

这里是代码

$('#postjob').click(function () {       
//Get the data from all the fields
var title = $('#jobtitle');
var date = $('#jobdate');
var status = $('#status');
var desc = $('#jobdesc');           

//Simple validation to make sure user entered something
//If error found, add error-highlight class to the text field

if (title.val()=='') {
    title.addClass('error-highlight'); 
    return false;
} else title.removeClass('error-highlight');

if (date.val()=='') {
    date.addClass('error-highlight');
    return false;
}

else date.removeClass('error-highlight');
if (desc.val()=='') {
    desc.addClass('error-highlight');
    return false;
}
else desc.removeClass('error-highlight');
var data;
if($("#jobid").val()=="")
{
    data = 'title=' + title.val() + '&date=' + date.val() + '&status=' + status.val() + '&desc=' + desc.val();  
}
else
    data = 'id=' + $("#jobid").val() + '&title=' + title.val() + '&date=' + date.val() + '&status=' + status.val() + '&desc=' + desc.val(); 
//organize the data properly

// Disable fields
//$('.text-label, .textarea-label').attr('disabled','true');
//show the loading sign
$('.loading-contact').show();
//start the ajax
$.ajax({
    //this is the php file that processes the data and send mail
    url: "postjob.php", 

    //GET method is used
    type: "POST",
    //pass the data         
    data: data,     
    //Do not cache the page
    cache: false,
    //success
    success: function (html) {          
        //if process.php returned 1/true (send mail success)
        if (html==1) {                  
        //hide the form
            //show the success message
            $('.loading-contact').fadeOut('slow');  
            //show the success message
            $('.success-message').slideDown('slow');
            $('.success-message').delay(1000).slideUp('slow');
            $('#jobsload').load("jobsload.php");

            // Disable send button
            //$('#send').attr('disabled',true);
            //if process.php returned 0/false (send mail failed)
        } else
        {
            $('.loading-contact').fadeOut('slow')
            alert('Sorry, unexpected error. Please try again later.');
        }
    }       
});
    //cancel the submit button default behaviours
    $('#accordion').accordion('destroy').accordion({ heightstyle: "content" });
    return false;
});

HTML 代码

<div id="jobsload" style="clear:both">
<div class="panel">
<div class="panel-heading"><center>Available Positions</center></div>
<div class="row">
    <?php
        $sql = "SELECT * FROM jobs where valid='YES'";
        $res = mysql_query($sql) or die(mysql_error());
    ?>
    <div class="personalInfo" id="accordion">   
        <?php while ($row = mysql_fetch_array($res)) 
        { ?>
            <h6 class="media-heading historyHeading">
                <span style="width:80%;"><a href="#"><?php echo $row['title'];?></a></span>
                <span style="width:20%;">(<?php echo $row['date'];?>)</span>
            </h6>
            <div>
                <p><?php echo $row['description'];?></p>
            </div>                              
        <?php } ?> 
    </div>
</div>
</div>

   <div class="panel">
<div class="panel-heading"><center>Positions Filled</center></div>
<div class="row">
    <?php
        $sql = "SELECT * FROM jobs where valid='NO'";
        $res = mysql_query($sql) or die(mysql_error());
    ?>
    <ul class="personalInfo">   
        <?php  $mycount=1; while ($row = mysql_fetch_array($res)) 
        { ?>
            <li>
                <span>
                    <div style="width:100%; border:thin #666666">
                        <div style="float:left; width:5%">
                            <p style="margin-left:10px; margin-top:5px" >
                                <?php echo $mycount; $mycount++; ?>
                            </p>
                        </div>
                        <div style="float:left; width:85%">
                            <h6 class="media-heading historyHeading">
                                <?php echo $row['title'];?>
                            </h6>
                        </div>
                        <div style="float:right; width:10%">
                            <h6 class="media-heading historyHeading">
                                <?php echo $row['date'];?>
                            </h6>
                        </div>
                    </div>
                </span>
                <div class="clearfix"></div>
            </li>
        <?php } ?> 
    </ul>
                    <!-- add this line to add small portfolio  -->
</div> 

感谢您的帮助。

【问题讨论】:

  • 我们可以看看你的手风琴的 HTML 吗?您的控制台是否显示任何错误?
  • @ICanHasCheezburger 您现在可以看到 HTML。

标签: php jquery ajax load accordion


【解决方案1】:

如果我是正确的,下面的代码会加载你的新内容:

$('#jobsload').load("jobsload.php"); 

而不是邮寄电话。

你需要重新初始化ajax加载的内容,因为它不在dom中,当jquery初始化时。

在 Kuma 的答案中,在调用负载的同时触发了手风琴。代码成功后不行。

查看下方代码使用jobsload的成功函数

$('#jobsload').load("jobsload.php", function( response, status, xhr ) {
    if (status == "success") {
      // Place reload the accordion code here
    }        
    if ( status == "error" ) {
      // optional: place error code here.
      // if you don't place this, user will not receive notification of failure.
    }
});

【讨论】:

  • 我认为“textStatus”应该只替换为“status”。我这样做了,然后在 if 块中放置了手风琴功能。 $('#accordion').accordion('destroy').accordion({ heightstyle: "content" });这是我应该做的吗?谢谢
  • 是的,我让我的状态不匹配,你纠正了我,我调整了代码。到目前为止,我知道(如果 $('#accordion').accordion('destroy').accordion({ heightstyle: "content" }); 是您要激活的代码)就足够了。你总是可以使用 alert('test');查看是否所有新内容都已加载。
  • taddaa!!谢谢你的帮助。 $('#accordion').accordion({heightstyle:"content"});在 if 块中编写是正确的。做好了!谢谢你:)
  • 很高兴为您服务! :)
【解决方案2】:

您应该在 success 函数中应用手风琴。

success: function (html) {          
        //if process.php returned 1/true (send mail success)
        if (html==1) {                  
        //hide the form
            //show the success message
            $('.loading-contact').fadeOut('slow');  
            //show the success message
            $('.success-message').slideDown('slow');
            $('.success-message').delay(1000).slideUp('slow');
            $('#jobsload').load("jobsload.php");

            // Disable send button
            //$('#send').attr('disabled',true);
            //if process.php returned 0/false (send mail failed)

            //cancel the submit button default behaviours
            $('#accordion').accordion('destroy').accordion({ heightstyle: "content" });
            return false;
        } else
        {
            $('.loading-contact').fadeOut('slow')
            alert('Sorry, unexpected error. Please try again later.');
        }
    }

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 2011-05-30
    • 2016-06-22
    • 1970-01-01
    • 2016-09-02
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多