字符串jquery的所有排列

时间:2014-06-14 16:17:40

标签: javascript jquery html5

我已经获得了stackoverflow上另一个用户的字符串的所有排列代码,我正在尝试使其在我的网页中运行。这段代码有问题吗?当我尝试提交单词时,我在javascript控制台中收到错误。错误会在短时间内闪烁,因此无法读取。

$(document).ready(function() {
    $('form#anagram').submit(function(event) {


function findAllPermutations(str, index, buffer) {
    if (typeof str == "string") {
        str = str.split("") }; 
    if (typeof index == "undefined") {
        index = 0 };
    if (typeof buffer == "undefined") {
        buffer = [] };
    if (index >= str.length) {
        return buffer };
    for (var i = index; i < str.length; i++) {
        buffer.push(ToggleLetters(str, index, i));
    }
    return FindAllPermutations(str, index + 1, buffer);
}

function ToggleLetters(str, index1, index2) {
    if (index1 != index2) {
        var temp = str[index1]
        str[index1] = str[index2]
        str[index2] = temp
    };
    return str.join("");
}



        list = $('input#word').val();
        $('#resulttext').append("<p>" + findAllPermutations(list) + "</p>")
        event.preventDefault();
    });
});

这是HTML:

<!DOCTYPE html>
<html>
<head>
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css">
    <link href="css/styles.css" rel="stylesheet" type="text/css">
    <script src="js/jquery-1.11.1.js"></script>
    <script src="js/scripts3.js"></script>
    <title>Anagram finder</title>
</head>
<body>
    <div class="container">
        <div class="band">
            <h1>Anagram Finder</h1>
        </div>

        <form id="anagram">
            <div class="form-group">
                <label for="word">Your Word</label>
                    <input id="word" type="text">

            </div>

            <button type="submit" class="btn">Find!</button>
        </form>
        <div id="resulttext">
        </div>

    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

函数名中有一个错误,并且event.preventDefault()位于函数的末尾,当页面被刷新时,您无法看到错误。

试一试: -

<!DOCTYPE html>

    

    <script src="jquery.js"></script>

    <title>Anagram finder</title>
</head>

<body>
    <script>
        $(document).ready(function() {
            $('form#anagram').submit(function(event) {

                event.preventDefault();
                function findAllPermutations(str, index, buffer) {
                    if (typeof str == "string") {
                        str = str.split("") }; 
                    if (typeof index == "undefined") {
                        index = 0 };
                    if (typeof buffer == "undefined") {
                        buffer = [] };
                    if (index >= str.length) {
                        return buffer };
                    for (var i = index; i < str.length; i++) {
                        buffer.push(ToggleLetters(str, index, i));
                    }
                    return findAllPermutations(str, index + 1, buffer);
                }

                function ToggleLetters(str, index1, index2) {
                    if (index1 != index2) {
                        var temp = str[index1]
                        str[index1] = str[index2]
                        str[index2] = temp
                    };
                    return str.join("");
                }



                list = $('input#word').val();
                $('#resulttext').append("<p>" + findAllPermutations(list) + "</p>")

            });
        });
    </script>
    <div class="container">
        <div class="band">
            <h1>Anagram Finder</h1>
        </div>

        <form id="anagram">
            <div class="form-group">
                <label for="word">Your Word</label>
                <input id="word" type="text">

            </div>

            <button type="submit" class="btn">Find!</button>
        </form>
        <div id="resulttext">
        </div>

    </div>
</body>
</html>