滑动元件不会滑落

发布于 2024-12-26 07:32:01 字数 1677 浏览 4 评论 0原文

我正在制作一个带有复选框的表单,如果选中该复选框,则会向下滑动更多选项,如果未选中,则会向上滑动。

对函数的调用有效一轮 IE 在选中时向下滑动,在取消选中时向上滑动,但不会向上滑动。

请帮我弄清楚如何进行这项工作。

**注意:我不了解 jQuery,目前不想投入时间来学习它。

<form>
<table>
rows and cells here
THIS IS THE CHECKBOX TO SHOW HIDDEN TABLE --> <input type="checkbox" name="booking" class="field" value="booking" onclick="show_booking('booking',200,5)"/> Check here for booking
</table>
<table id="booking">
HIDDEN rows and cells here
</table>
</form>

JAVASCRIPT***

function show_booking(obj, heightOpen, heightClose){
    if(document.getElementById(obj).style.height <= 6 ){
        animateDown(document.getElementById(obj),heightOpen);
    }
    else {
        animateUp(document.getElementById(obj), heightClose);
        
    }
}

function animateDown(obj, height){
    
       var obj_height = obj.clientHeight;
       
       if(obj_height >= height){ return;}               
       else {
           obj.style.height = (obj_height + 5) + "px";
           setTimeout(function(){
               animateDown(obj, height);
           }, 25)
       }
}
    

function animateUp (obj, height){

       var obj_height = obj.style.height.substring(0,2);
        if(obj_height >= height){    obj.style.height = (obj_height - 5) + "px";
           setTimeout(function(){
               animateUp(obj, height);
           }, 200)}   
       else { return; }         
       }    

I'm making a form that has a checkbox that if checked will slide down some more options, and unchecked it will slide up.

The call to the function works one round I.E. slide down on check and up on uncheck but wont slide back up.

Please help me figure out how to make this work.

**NOTE: I don't know jQuery and don't want to invest the time to learn it at this point.

<form>
<table>
rows and cells here
THIS IS THE CHECKBOX TO SHOW HIDDEN TABLE --> <input type="checkbox" name="booking" class="field" value="booking" onclick="show_booking('booking',200,5)"/> Check here for booking
</table>
<table id="booking">
HIDDEN rows and cells here
</table>
</form>

JAVASCRIPT***

function show_booking(obj, heightOpen, heightClose){
    if(document.getElementById(obj).style.height <= 6 ){
        animateDown(document.getElementById(obj),heightOpen);
    }
    else {
        animateUp(document.getElementById(obj), heightClose);
        
    }
}

function animateDown(obj, height){
    
       var obj_height = obj.clientHeight;
       
       if(obj_height >= height){ return;}               
       else {
           obj.style.height = (obj_height + 5) + "px";
           setTimeout(function(){
               animateDown(obj, height);
           }, 25)
       }
}
    

function animateUp (obj, height){

       var obj_height = obj.style.height.substring(0,2);
        if(obj_height >= height){    obj.style.height = (obj_height - 5) + "px";
           setTimeout(function(){
               animateUp(obj, height);
           }, 200)}   
       else { return; }         
       }    

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

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

发布评论

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

评论(1

南烟 2025-01-02 07:32:01

您实际上是在尝试根据复选框是否被选中来采取行动,所以为什么不使用复选框的选中属性:

<input onclick="show_booking.apply(this, ['booking', 200, 5])" type="checkbox" name="booking" class="field" value="booking" 

apply将函数内部的this值设置为第一个争论。因此,在 show_booking this 内部将是刚刚单击的复选框。现在您可以更简单地编写该函数,例如:

function show_booking(obj, heightOpen, heightClose){
    if(this.checked) {
        animateDown(document.getElementById(obj),heightOpen);
    } 
    else {
        animateUp(document.getElementById(obj), heightClose);
    }
}

You're really trying to act based on whether the checkbox is checked, so why not use the checkbox's checked property:

<input onclick="show_booking.apply(this, ['booking', 200, 5])" type="checkbox" name="booking" class="field" value="booking" 

apply sets the this value inside of the function to the first argument. So inside of show_booking this will be the checkbox that was just clicked. Now you can more simply write the function like:

function show_booking(obj, heightOpen, heightClose){
    if(this.checked) {
        animateDown(document.getElementById(obj),heightOpen);
    } 
    else {
        animateUp(document.getElementById(obj), heightClose);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文