Php Jquery在表单提交之间隐藏/显示

Php Jquery在表单提交之间隐藏/显示,php,javascript,jquery,twitter-bootstrap,Php,Javascript,Jquery,Twitter Bootstrap,我有一个表单,它由一个循环弹出,它的目的是以可管理的方式显示车辆清单,所以您可以编辑其中的任何内容 我的问题是,当我点击“隐藏/显示”按钮时,它会提交表单,因为它是空的,所以它会将我重定向到主页 它过去是有用的。当我允许我的代码从google获取jquery时,它实际上是有效的 我确信这不是根本问题,但似乎可以解决 这是我的表格代码 <?php function manage_vehicles($soldvalue) { $actual_link = "http://$_SERVE

我有一个表单,它由一个循环弹出,它的目的是以可管理的方式显示车辆清单,所以您可以编辑其中的任何内容 我的问题是,当我点击“隐藏/显示”按钮时,它会提交表单,因为它是空的,所以它会将我重定向到主页

它过去是有用的。当我允许我的代码从google获取jquery时,它实际上是有效的 我确信这不是根本问题,但似乎可以解决

这是我的表格代码

<?php
function manage_vehicles($soldvalue)
{
    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    echo '<div class="span12">
    <a href=""><button id="deleteall" name="deleteall" class="btn btn-danger" type="submit" >Delete</button></a> &nbsp;';
    if ($actual_link == 'http://localhost/cardealerwins/admin.php?manage_vehicles')
    {
        echo '<a href="admin.php?manage_sold"><button href="admin.php?manage_sold" id="managesold" name="deleteall" 
        class="btn btn-success" type="submit" >Manage sold vehicles</button></a>';
    } 
    else if ($actual_link == 'http://localhost/cardealerwins/admin.php?manage_sold')
    {
        echo '<a href="admin.php?manage_vehicles"><button href="admin.php?manage_vehicles" id="managesold" name="deleteall" 
        class="btn" type="submit" >Manage active vehicles</button></a>';
    }
    echo '</div>';
    //get the total number of vehicles active in the database to loop through them and displaying them to manage
    $query = mysql_query("SELECT COUNT('auto_id') FROM `auto` WHERE `sold` = $soldvalue");
    $all = mysql_result($query, 0);  //store the value so we can run this loop accordingly
    $counter = 1;
    $auto_id = 0; //auto id equal to zero as starting point, it will increase randomly to as many vehicles it will diplay
    while ($counter <= $all)  //ie run for every car
    {
        $fields = 'auto_id, year, make, model, engine, sound_system,mileage, price, vin, att1, att2, att3, att4, att5, att6, att7, att8, 
        att9, att10, att11, att12, att13, att14, att15, picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, 
        picture9, picture10, picture11, picture12';
        //check where the auto id is greater then the previously one according to $auto_id which increments
        $data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `auto` WHERE `auto_id` > $auto_id AND `sold` = $soldvalue"));

        $auto_id = $data['auto_id'];  //here is the magic, we set the auto id to the current one being spitted out so it follows through
        //now with all the $data retrieved from the database we can print car by car with its unique info and ids
        echo '<div id="displaycarwrap" class="span3 container">
        <input type="checkbox" value="'. $data['auto_id'] .'" class="deletebox" /><br />
        <a href="#"><img src="' . $data['picture1'] . '"></a>
        <form action="admin/tool/editvehicle.php" method="post">
            <ul>
                <li>
                    <br><b>Price</b>: <input name="price" type="text" class="span2" value="' . $data['price'] . '">
                </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 btn btn-mini icon-th-list" href="#" rel="slidingDiv'. $counter .'">View details</button>
            <div id="slidingDiv'. $counter .'" class="toggleDiv" style="display: none;">
            <ul> <br />
                <li><b>Year</b>:<br /><input type="text" name="year" value="' . $data['year'] . '" class="span2"></li>
                <li><b>Make</b>: <br /><input type="text" name="make" value="' . $data['make'] . '" class="span2"></li>
                <li><b>Model</b>: <br /><input type="text" name="model" value="' . $data['model'] . '" class="span2"></li>
                <li><b>Mileage</b>: <br /><input type="text" name="mileage" value="' . $data['mileage'] . '" class="span2"></li>
                <li><b>Engine</b>:<br /> <input type="text" name="engine" value="' . $data['engine'] . '" class="span2"></li>
                <li><b>Stereo</b> <br /><input type="text" name="sound_system" value="' . $data['sound_system'] . '" class="span2"></li>
                <li><b>Attribute 1 </b> <input type="text" name="att1" value="' . $data['att1'] . '" class="span2"></li>
                <li><b>Attribute 2</b> <input type="text" name="att2" value="' . $data['att2'] . '" class="span2"></li>
                <li><b>Attribute 3</b> <input type="text" name="att3" value="' . $data['att3'] . '" class="span2"></li>
                <li><b>Attribute 4</b> <input type="text" name="att4" value="' . $data['att4'] . '" class="span2"></li>
                <li><b>Attribute 5</b> <input type="text" name="att5" "value="' . $data['att5'] . '" class="span2"></li>
                <li><b>Attribute 6</b> <input type="text" name="att6" value="' . $data['att6'] . '" class="span2"></li>
                <li><b>Attribute 7</b> <input type="text" name="att7" value="' . $data['att7'] . '" class="span2"></li>
                <li><b>Attribute 8</b> <input type="text" name="att8" value="' . $data['att8'] . '" class="span2"></li>
                <li><b>Attribute 9</b> <input type="text" name="att9" value="' . $data['att9'] . '" class="span2"></li>
                <li><b>Attribute 10</b> <input type="text" name="att10" value="' . $data['att10'] . '" class="span2"></li>
                <li><b>Attribute 11</b> <input type="text" name="att11" value="' . $data['att11'] . '" class="span2"></li>
                <li><b>Attribute 12</b> <input type="text" name="att12" value="' . $data['att12'] . '" class="span2"></li>
                <li><b>Attribute 13</b> <input type="text" name="att13" value="' . $data['att13'] . '" class="span2"></li>
                <li><b>Attribute 14</b> <input type="text" name="att14" value="' . $data['att14'] . '" class="span2"></li>
                <li><b>Attribute 15</b> <input type="text" name="att15" value="' . $data['att15'] . '" class="span2"></li>
                <li><b>Vin</b> <input type="text" name="vin" value="' . $data['vin'] . '" class="span2"></li>
                <li><input type="text" name="auto_id" value="' . $data['auto_id'] . '" class="span2" readonly><li>
            <ul>

            </div>
        </form>
        </div>';
        $counter++; //used for the while loop it self and for classes id uniqueness for javascript
    }
}

?>
我的控制台告诉我:

Uncaught ReferenceError: google is not defined 
我不认为这与此无关

未捕获引用错误的错误

Uncaught ReferenceError: google is not defined
jQuery.fn.extend.addEventListener
jQuery.fn.(anonymous function)
$.fn.showHide managevehicles.js:20
(anonymous function) managevehicles.js:46
fire jquery2.js:3074
self.fireWith jquery2.js:3186
jQuery.extend.ready jquery2.js:433
completed jquery2.js:104
Uncaught ReferenceError: google is not defined
jQuery.fn.extend.addEventListener
jQuery.fn.(anonymous function)
(anonymous function) scripts.js:10
jQuery.event.dispatch jquery2.js:5116
elemData.handle jquery2.js:4787

您正在使用的“显示/隐藏”按钮在默认情况下用作“提交”按钮。事实上,任何 表单中的元素将在单击时提交表单

试着改变

    <button class="show_hide btn btn-mini icon-th-list" href="#" rel="slidingDiv'. $counter .'">View details</button>

to <input type="button"> 

您可以使用event.preventDefault方法取消url重定向,如下所示:

$(this).click(function (event) {
  //Cancel the url redirection caused by clicking the a tag.
  event.preventDefault();

// 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文档中的示例,了解preventDefault

只是一个侧面:不要在函数中多次使用$this。将其存储在一个变量中,而通常称为$this。现在,每次调用jQuery时,它都会处理这一切。如果你说它曾经工作过,我开始质疑你的DB/PHP。这里的“auto_id”>$auto_id和-你能解释一下>?@Fred,它只是确保下一个auto id至少比上一个大吗looped@YoungStudent好吧,我想是这样的,但不是完全确定。自从PHP和/或DB停止工作以来,它有什么变化吗?或者你确信是jQuery出了问题吗?google uncaught刚刚透露,是的,这是一个jQuery问题,我正在努力解决。不客气,Sasanka。在谷歌搜索了一段时间后,我发现OP对$\u服务器语句的使用确实是有效的,就像我的错误一样。干杯,我把按钮换成了输入按钮,但它不起作用。我用的是bootstrap,这可能正在进行中,我正准备试用它js@YoungStudent尝试使用我提供的JQuery代码禁用默认值submission@SasankaPanguluri我想知道这是否与OP到所有JS的路径有关。根据我在谷歌上能找到的。一个页面正在异步加载,或者可能需要异步加载。@Fred不知道,我无法从OP的代码中了解太多内容。我无法找出引用错误发生的位置。如果OP可以发布所有的代码,它会更清晰
$('.show_hide').click(function(e){
     e.preventDefault();
});
$(this).click(function (event) {
  //Cancel the url redirection caused by clicking the a tag.
  event.preventDefault();

// 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;

        });