douzi2749 2013-08-15 00:24
浏览 47
已采纳

JQuery隐藏和显示脚本在窗体内不起作用

I have a function in jQuery that hides and shows a div, it used to work perfectly.

But now isn't showing, it stays hidden or sometimes with different jQuery versions it submits the form and I dont know where my error is.

I'm running the latest jQuery version, and I'm also running bootstrap so it could be getting in the way.

Here is a jsFiddle where the form gets submitted when clicked, I expierence that right now. http://jsfiddle.net/d3fNj/

Function:

//function to hide the extra info for the cars in manage vehicles
(function ($) {
    $.fn.showHide = function (options) {

    //default vars for the plugin
        var defaults = {
            speed: 250,
            easing: '',
            changeText: 0,
            showText: 'View Details',
            hideText: 'close'

        };
        var options = $.extend(defaults, options);

        $(this).click(function () {
                // optionally add the class .toggleDiv to each div you want to automatically close
                      $('.toggleDiv:hidden').slideUp(options.speed, options.easing);
             // this var stores which button you've clicked
             var toggleClick = $(this);
             // this reads the rel attribute of the button to determine which div id to toggle
             var toggleDiv = $(this).attr('rel');
             // here we toggle show/hide the correct div at the right speed and using which easing effect
             $(toggleDiv).slideToggle(options.speed, options.easing, function() {
             // this only fires once the animation is completed
             if(options.changeText==1){
             $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
             }
              });

          return false;

        });

    };
})(jQuery);



$(document).ready(function(){

   $('.show_hide').showHide({
        speed: 250,  // speed you want the toggle to happen
        easing: '',  // the animation effect you want. Remove this line if you dont want an effect and if you haven't included jQuery UI
        changeText: 1, // if you dont want the button text to change, set this to 0
        showText: 'View Details',// the button text to show when a div is closed
        hideText: 'Close' // the button text to show when a div is open

    });
});

Here is the HTML:

<form action="admin/tool/editvehicle.php" method="post">
    <ul>
        <li>
            <br><b>Price</b>: <input name="price" type="text" class="span2" value="14699">
        </li>
        <li>
            <button name="delete" class="btn btn-danger" type="submit" >Delete Vehicle</button>
        </li>
        <li>
            <button name="sold" class="btn btn-success" type="submit" >Mark as Sold</button>
        </li>
        <li>
            <button class="btn btn-primary" name="editpictures" type="submit" >Edit Pictures</button>
        </li>
        <li>
            <button type="submit" name="editauto" class="btn  btn-inverse" >Apply Changes</button>
        </li>
    </ul>
    <button class="show_hide" href="#" rel="slidingDiv1">View Details</button>
    <div id="slidingDiv1" class="toggleDiv" style="display: none;">
    <ul> <br />
        <li><b>Year</b>:<br /><input type="text" name="year" value="2011" class="span2"></li>
        <li><b>Make</b>: <br /><input type="text" name="make" value="Acura" class="span2"></li>
        <li><b>Model</b>: <br /><input type="text" name="model" value="TCi" class="span2"></li>
        <li><b>Mileage</b>: <br /><input type="text" name="mileage" value="135222" class="span2"></li>
        <li><b>Engine</b>:<br /> <input type="text" name="engine" value="5.0" class="span2"></li>
        <li><b>Stereo</b> <br /><input type="text" name="sound_system" value="beats audio" class="span2"></li>
        <li><b>Attribute 1 </b> <input type="text" name="att1" value="adfasdf" class="span2"></li>
        <li><b>Attribute 2</b> <input type="text" name="att2" value="" class="span2"></li>
        <li><b>Attribute 3</b> <input type="text" name="att3" value="" class="span2"></li>
        <li><b>Attribute 4</b> <input type="text" name="att4" value="" class="span2"></li>
        <li><b>Attribute 5</b> <input type="text" name="att5" "value="" class="span2"></li>
        <li><b>Attribute 6</b> <input type="text" name="att6" value="" class="span2"></li>
        <li><b>Attribute 7</b> <input type="text" name="att7" value="" class="span2"></li>
        <li><b>Attribute 8</b> <input type="text" name="att8" value="" class="span2"></li>
        <li><b>Attribute 9</b> <input type="text" name="att9" value="" class="span2"></li>
        <li><b>Attribute 10</b> <input type="text" name="att10" value="" class="span2"></li>
        <li><b>Attribute 11</b> <input type="text" name="att11" value="" class="span2"></li>
        <li><b>Attribute 12</b> <input type="text" name="att12" value="" class="span2"></li>
        <li><b>Attribute 13</b> <input type="text" name="att13" value="" class="span2"></li>
        <li><b>Attribute 14</b> <input type="text" name="att14" value="" class="span2"></li>
        <li><b>Attribute 15</b> <input type="text" name="att15" value="" class="span2"></li>
        <li><b>Vin</b> <input type="text" name="vin" value="FASDFA23423ASDF" class="span2"></li>
        <li><input type="text" name="auto_id" value="59" class="span2" readonly><li>
    <ul>

    </div>
</form>
  • 写回答

1条回答 默认 最新

  • douyi5157 2013-08-15 00:31
    关注

    I loaded jQuery in the fiddle and changed this line:

    <button class="show_hide" href="#" rel="slidingDiv1">View Details</button>
    

    to:

    <button class="show_hide" rel="#slidingDiv1">View Details</button>
    

    and it's working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像