我已经从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>

最佳答案

函数名称中有一个错误,并且由于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>

关于javascript - 字符串jquery的所有排列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24221881/

10-11 13:38