javascript - 这里我想在ajax中加载数据 hide show div

标签 javascript jquery html ajax

我想使用ajax显示或隐藏数据div

这里我的代码作为脚本工作,但我想像在 ajax 中一样加载。

如果点击下一个按钮,它会显示下一个 div。

然后单击上一个按钮,它会显示上一个 div。

我的脚本代码如下:

<script>
$('#next').click(function() {
$('.current').removeClass('current').hide()
    .next().show().addClass('current');
if ($('.current').hasClass('last')) {
    $('#next').attr('disabled', true);
}
$('#prev').attr('disabled', null);
});

$('#prev').click(function() {
$('.current').removeClass('current').hide()
    .prev().show().addClass('current');
if ($('.current').hasClass('first')) {
    $('#prev').attr('disabled', true);
}
$('#next').attr('disabled', null);
});
</script>

我的html代码如下:

<h1> QUESTIONS</h1>

<style>
#div{display:none;}
</style>
<button id="prev" disabled="disabled"> PREVIOUS </button>
<button id="next"> NEXT </button>

<form action="<?php echo site_url('pages/answers');?>" method="post">

<div id="main"> 
<?php $i=1; 
$tt=$ques->num_rows();
foreach($ques->result() as $qus)
{   ?>  
<div <?php if($i==1){?>id="div1" class="first current" <?php } else {?> id="div" <?php } if($i==$tt) {?> class="last"<?php }?>>
<input type="hidden" name="q_id[]" value="<?php echo $qus->qst_id; ?>"/> 
  <?php  echo $i.') ' .$qus->qst_questions;?>
   <label>  <input type="radio" name="<?php echo $qus->qst_id;?>" value="A"/><?php echo $qus->qst_option_a; ?></label>
   <label>  <input type="radio" name="<?php echo $qus->qst_id;?>" value="B"/><?php echo $qus->qst_option_b; ?></label>
   <label>  <input type="radio" name="<?php echo $qus->qst_id;?>" value="C"/><?php echo $qus->qst_option_c; ?></label>
   <label>  <input type="radio" name="<?php echo $qus->qst_id;?>" value="D"/><?php echo $qus->qst_option_d; ?></label>

</div>

<?php $i++; }?>
</div>

</form>

这里使用ajax显示/隐藏div。

如何做到这一点?请帮助任何人解决我的问题。

这里作为一个例子['plese see this ']

这是在脚本中加载的,但我想在ajax中加载

最佳答案

更新您的 JavaScript: 更新您的 AJAX 配置,它应该可以工作。

Demo

$('#next').click(function() {
  $('.current').removeClass('current').hide()
    .next().show().addClass('current');
  if ($('.current').hasClass('last')) {
    $('#next').attr('disabled', true);
  }
  $('#prev').attr('disabled', null);
  $.ajax({
    url: "Url",
    success: function(result) {
      $('#main div:visible').html(result);

    },
    error: function(err) {
      console.log($('#main div:visible').attr('id'));
      $('#main div:visible').html('dynamic html content');
    }
  });

});

$('#prev').click(function() {
  $('.current').removeClass('current').hide()
    .prev().show().addClass('current');
  if ($('.current').hasClass('first')) {
    $('#prev').attr('disabled', true);
  }
  $('#next').attr('disabled', null);
  $.ajax({
    url: "Url",
    success: function(result) {
      $('#main div:visible').html(result);
    },
    error: function(err) {
      console.log($('#main div:visible').attr('id'));
      $('#main div:visible').html('dynamic html content');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<button id="prev" disabled="disabled">Prev</button>
<button id="next">Next</button>
<hr />
<div id="main">
  <div id="div1" class="first current">Div 1</div>
  <div id="div2">Div 2</div>
  <div id="div3" class="last">Div 3</div>
</div>

关于javascript - 这里我想在ajax中加载数据 hide show div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763199/

相关文章:

javascript - 用纯 javascript 覆盖全局 css

jQuery 禁用所有重定向(链接、表单提交、window.location 更改等)

html - IE9 滚动错误(边界线在屏幕上重复)

HTML/CSS Tumblr : How to add a new line after each link in Masthead?

javascript - 语法错误 : Illegal return statement - searching for string in url

javascript - 随机播放声音重叠

javascript - 为不同线程的 Disqus 评论创建动态弹出窗口

ruby-on-rails-3 - 在 js.erb 内渲染部分内容。是否可以获取原始字符串以便去除空格?

javascript - 从配置文件插入 HTML 元素的更简单和更可维护的方法

php - 使用ajax使用已从数据库检索的数据获取更多mysql数据