【问题标题】:Here i want to load data in ajax hide show div在这里我想在 ajax hide show div 中加载数据
【发布时间】:2015-04-21 04:53:10
【问题描述】:

我想使用 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 中加载

【问题讨论】:

  • 您想通过 AJAX 加载内容,然后在选项卡中显示。对吗?
  • Tushar..你看到了上面的例子,我想在 ajax 中加载
  • @YuvarajUraj “加载 ajax”是什么意思?你需要在ajax中加载div的内容吗?

标签: javascript jquery html ajax


【解决方案1】:

更新您的 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>

【讨论】:

  • @我点击下一个或上一个按钮时得到dynamic html content
  • @YuvarajUraj 这就是我所说的:Update your configurations for AJAX and it should work. 在您的代码中尝试相同的操作。
  • 如果它是可见的 ajax 加载意味着它不会在 view source 页面中显示 div 内容 code 。这里显示了 div 内容
  • @YuvarajUraj 你的意思是你必须为每个 AJAX 请求创建一个新的 div 元素?您可以在$.ajaxsuccess 回调中执行此操作。
  • 是的,我知道了 tushar,我们已经接近答案了,它可以点击下一步,我配置了它,但它显示在 view source code 我不知道它在哪里提供请你完成并更新小提琴并发送给我
【解决方案2】:
$('#next').click(function() {
    $.ajax({url: "Url", 
            success: function(result){
         $('.current').removeClass('current').hide()
                .next().show().addClass('current');
            if ($('.current').hasClass('last')) {
                $('#next').attr('disabled', true);
            }
            $('#prev').attr('disabled', null);
    }});

});

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

});

请在成功部分写下你的代码 你可以从服务器端返回值将在“结果”变量中到达 您可以使用“结果”进行检查

【讨论】:

  • @YuvarajUraj 这不是您个人调试的地方 - 如果它不起作用,请指出控制台所说的内容
  • console 我不能得到任何错误,所以只有我在这里提到。你有任何答案意味着提供给我.....@#Nicholas Kyriakides#@
  • 你给出的不是“url”..我没有给出确切的代码..它只是一个你必须根据你的代码修改的示例
  • 我不能在 url 中提供,因为我在这里不做任何操作,所以只有我不能提供任何东西。请 sreerejith 提供完整的代码或小提琴示例或提供说明
  • 这个想法是,如果你想调用 ajax 那就是这样做的方法 $.ajax({url: "Url", success: function(result){ your code.. }});
猜你喜欢
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-19
相关资源
最近更新 更多