检查 div 是否是最后一个并显示按钮

发布于 2024-09-14 13:10:53 字数 2699 浏览 3 评论 0原文

嘿伙计们。我有一个测验脚本,在不同的 div 中有问题,它们一个接一个地出现,我希望当最后一个 div 淡出时,它会显示提交按钮而不是下一个问题按钮。

这是我的js。

    $('#questions').ready(function(){

    for (i = 1; i < 100; i++) {
      if ($('#question' + i).length > 0) {
            $('#question' + i).hide();


  } else {
            break;
        }

  }



});


function transition(question_id) {

    $('#question' + question_id).fadeOut('fast',function() {
           question_id++;
        if ($('#question' + question_id).length > 0) {
           $('#question' + question_id).fadeIn('fast');

        } else {
            $('#test').submit();
  } return false;

    });

}

这是我的 html :

<div class="questions"> 
        <div id="question0" class="question">
<span class="qutitle">asdg asdg</span> <br /> 

         <FIELDSET> 


               <br />   <input id="q_0" type="radio" name="q_0" value="0"> sdgas                       <br /> 



               <br />   <input id="q_0" type="radio" name="q_0" value="2">dgasdg                       <br /> 



               <br />   <input id="q_0" type="radio" name="q_0" value="1">dgas                       <br /> 

</FIELDSET><input type="button"  onclick="javascript: transition(0);" value="">
</div> 
        <div id="question1" class="question"><span class="qutitle">asdgasd</span> <br /> 

         <FIELDSET> 


               <br />   <input id="q_1" type="radio" name="q_1" value="0">gasdg                       <br /> 



               <br />   <input id="q_1" type="radio" name="q_1" value="2">asdgasdg                       <br /> 



               <br />   <input id="q_1" type="radio" name="q_1" value="1">sdgasdg                       <br /> 

</FIELDSET><input type="button"  onclick="javascript: transition(1);" value="">
</div> 
        <div id="question2" class="question"><span class="qutitle">sgasdgasd</span> <br /> 

         <FIELDSET> 


               <br />   <input id="q_2" type="radio" name="q_2" value="1">asdgsad                       <br /> 



               <br />   <input id="q_2" type="radio" name="q_2" value="2">gasdg                       <br /> 



               <br />   <input id="q_2" type="radio" name="q_2" value="0">gasdg                       <br /> 

</FIELDSET><input type="button"  onclick="javascript: transition(2);" value=""></div>

感谢任何形式的帮助。

谢谢。

Hey guys. I have a quiz script, that has questions in different divs, they come one after another, and I want that when the last div fadesIn it would show submit button instead of a next question button.

Here's my js.

    $('#questions').ready(function(){

    for (i = 1; i < 100; i++) {
      if ($('#question' + i).length > 0) {
            $('#question' + i).hide();


  } else {
            break;
        }

  }



});


function transition(question_id) {

    $('#question' + question_id).fadeOut('fast',function() {
           question_id++;
        if ($('#question' + question_id).length > 0) {
           $('#question' + question_id).fadeIn('fast');

        } else {
            $('#test').submit();
  } return false;

    });

}

Here's my html :

<div class="questions"> 
        <div id="question0" class="question">
<span class="qutitle">asdg asdg</span> <br /> 

         <FIELDSET> 


               <br />   <input id="q_0" type="radio" name="q_0" value="0"> sdgas                       <br /> 



               <br />   <input id="q_0" type="radio" name="q_0" value="2">dgasdg                       <br /> 



               <br />   <input id="q_0" type="radio" name="q_0" value="1">dgas                       <br /> 

</FIELDSET><input type="button"  onclick="javascript: transition(0);" value="">
</div> 
        <div id="question1" class="question"><span class="qutitle">asdgasd</span> <br /> 

         <FIELDSET> 


               <br />   <input id="q_1" type="radio" name="q_1" value="0">gasdg                       <br /> 



               <br />   <input id="q_1" type="radio" name="q_1" value="2">asdgasdg                       <br /> 



               <br />   <input id="q_1" type="radio" name="q_1" value="1">sdgasdg                       <br /> 

</FIELDSET><input type="button"  onclick="javascript: transition(1);" value="">
</div> 
        <div id="question2" class="question"><span class="qutitle">sgasdgasd</span> <br /> 

         <FIELDSET> 


               <br />   <input id="q_2" type="radio" name="q_2" value="1">asdgsad                       <br /> 



               <br />   <input id="q_2" type="radio" name="q_2" value="2">gasdg                       <br /> 



               <br />   <input id="q_2" type="radio" name="q_2" value="0">gasdg                       <br /> 

</FIELDSET><input type="button"  onclick="javascript: transition(2);" value=""></div>

Any kind of help is appreciated.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-09-21 13:10:54
questions = $('.question').length; // total number of questions

然后我想你可以这样做:

if(questions-1 == question_id){
    //Show submit button & hide next button
}

另外,在一开始,你可以只做 $('.questions').hide(),而不是 for 循环。

questions = $('.question').length; // total number of questions

Then I guess you could do something like:

if(questions-1 == question_id){
    //Show submit button & hide next button
}

Also, in the beginning, you could just do $('.questions').hide(), instead of the for loop.

北斗星光 2024-09-21 13:10:54

$('div[id *= "question"]:last') 将获取 id 上带有“question”的最后一个 div。

编辑:

要显示按钮,您可以这样做:

$('div[id *= "question"]:last').children('input[type == "button"]).fadeIn('fast')

$('div[id *= "question"]:last') will get the last div with "question" on the id.

Edit:

To show the button, you can do this:

$('div[id *= "question"]:last').children('input[type == "button"]).fadeIn('fast')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文