AJAX调用不适用于jQuery切换

时间:2014-01-22 19:38:48

标签: javascript jquery ajax

试图让我的jQuery切换更易于管理;我试图通过一个AJAX调用成功地获取它的内容,但我的AJAX调用会杀死我的切换 - 任何有关我在这里做错的指示?

<script>    
    $(document).ready(function() {    
        $('#toggle3').click(function(){
            var $tog = $('.toggle');
            $tog.hide(1000);
            $.ajax({
                url: 'path/to/my/script.php',
                type: 'GET', //this is default anyway, only for verbosity
                success: function (fields){
                    $tog.html(fields);
                    $tog.slideToggle(1000);
                }
            });
        });
    });    
</script>

Script.php 是一个基本上嵌套在div中的表单;如下所示:

<div style="font-size: 12px; color: #000; text-align: left; padding-left: 15px; padding-top: 20px;">
    <form>
        <br>Back wheel color?
        <br>
        <input type="radio" name="backwheel" value="Purple"><span style="color: #B500E4">Purple</span>
        <br>
        <input type="radio" name="backwheel" value="White">White</br>
        <input type="radio" name="backwheel" value="Light Blue"><span style="color: #74A1C4;">Light Blue</span>
        </br>
        <input type="radio" name="backwheel" value="Blue">Blue</br>
        <input type="radio" name="backwheel" value="Tan">Tan</br>
        <input type="radio" name="backwheel" value="Grey">Grey</br>
        <input type="radio" name="backwheel" value="Pink">Pink</br>
        <input type="radio" name="backwheel" value="Red">Red</br>
        <input type="radio" name="backwheel" value="Yellow">Yellow</br>
        <input type="radio" name="backwheel" value="Black">Black</br>
        <input type="radio" name="backwheel" value="Green"><span style="color:#44CA2C">Green</span>
        </br>
    </form>
    </span>
    </form>
    <br>Front Wheel (if different)
    <br>
    <form>
        <br>Front wheel color?
        <br>
        <input type="radio" name="backwheel" value="Purple"><span style="color: #B500E4">Purple</span>
        <br>
        <input type="radio" name="backwheel" value="White">White</br>
        <input type="radio" name="backwheel" value="Light Blue"><span style="color: #74A1C4;">Light Blue</span>
        </br>
        <input type="radio" name="backwheel" value="Blue">Blue</br>
        <input type="radio" name="backwheel" value="Tan">Tan</br>
        <input type="radio" name="backwheel" value="Grey">Grey</br>
        <input type="radio" name="backwheel" value="Pink">Pink</br>
        <input type="radio" name="backwheel" value="Red">Red</br>
        <input type="radio" name="backwheel" value="Yellow">Yellow</br>
        <input type="radio" name="backwheel" value="Black">Black</br>
        <input type="radio" name="backwheel" value="Green"><span style="color:#44CA2C">Green</span>
        </br>
    </form>
</div>
<div id="next"><a href="#" id="toggle3">Check Out!<img src="http://northbrooklyncollective.com/wp-content/uploads/2013/11/519629-129_ArrowRight-128.png" class="tool"></a>
</div>

最初正在运行的代码(如下):

$(function() {
    $('#toggle3').click(function () {
        $('.toggle').hide('1000');
        $('.toggle').html('<div style="font-size: 12px; color: #000; text-align: left; padding-left: 15px; padding-top: 20px;"><form><br>Back wheel color?<br><input type="radio" name="backwheel" value="Purple"><span style="color: #B500E4"><img src="http://ecx.images-amazon.com/images/I/41S7CRzpV3L._AA160_.jpg" style="max-height: 100px;">Purple</span><br><input type="radio" name="backwheel" value="White">White</br><input type="radio" name="backwheel" value="Light Blue"><span style="color: #74A1C4;">Light Blue</span></br><input type="radio" name="backwheel" value="Blue">Blue</br><input type="radio" name="backwheel" value="Tan">Tan</br><input type="radio" name="backwheel" value="Grey">Grey</br><input type="radio" name="backwheel" value="Pink">Pink</br><input type="radio" name="backwheel" value="Red">Red</br><input type="radio" name="backwheel" value="Yellow">Yellow</br><input type="radio" name="backwheel" value="Black">Black</br><input type="radio" name="backwheel" value="Green"><span style="color:#44CA2C">Green</span></br></form></span></form><br>Front Wheel (if different)<br><form><br>Front wheel color?<br><input type="radio" name="backwheel" value="Purple"><span style="color: #B500E4">Purple</span><br><input type="radio" name="backwheel" value="White">White</br><input type="radio" name="backwheel" value="Light Blue"><span style="color: #74A1C4;">Light Blue</span></br><input type="radio" name="backwheel" value="Blue">Blue</br><input type="radio" name="backwheel" value="Tan">Tan</br><input type="radio" name="backwheel" value="Grey">Grey</br><input type="radio" name="backwheel" value="Pink">Pink</br><input type="radio" name="backwheel" value="Red">Red</br><input type="radio" name="backwheel" value="Yellow">Yellow</br><input type="radio" name="backwheel" value="Black">Black</br><input type="radio" name="backwheel" value="Green"><span style="color:#44CA2C">Green</span></br></form></div><div id="next"><a href="#" id="toggle3">Check Out!<img src="http://northbrooklyncollective.com/wp-content/uploads/2013/11/519629-129_ArrowRight-128.png" class="tool"></a></div>');
        $('.toggle').slideToggle('1000');

        return false; 
    });
});

1 个答案:

答案 0 :(得分:1)

在这种情况下的问题是您的变量名称($ tog)以美元符号开头。虽然这在技术上是一个有效的Javascript变量名称,但它与jQuery库相冲突,jQuery库也以$开头。

虽然PHP使用$作为变量名,但这在Javascript中并不标准。

您可以按如下方式更新代码以解决:

<script>

$(document).ready(function() {

$('#toggle3').click(function(){
    var tog = $('.toggle');
    tog.hide(1000);
    $.ajax({
        url: 'path/to/my/script.php',
        type: 'GET', //this is default anyway, only for verbosity
        success: function (fields){
            tog.html(fields);
            tog.slideToggle(1000);
        }
    });
  });
});

</script>