D
D
Denis Bredun2020-08-24 20:25:03
JavaScript
Denis Bredun, 2020-08-24 20:25:03

Why, instead of calling mySearch() method (see code and description), does it go to a page with the same url-address, but completely white?

Here is the code:

@page
@{
    Layout = "_Layout";
}
@model Inst_Post_Searcher.Pages.HomePageModel
<!DOCTYPE html>
<html>
<head>
    <style>
        #searchsentenсe {
            margin-left: 12%;
            height: 3.5%;
            font-size: 0.9vw;
            background-color: #f5ba42;
            border-color: black;
            -moz-border-radius: 3px;
            -webkit-border-radius: 3px;
            border-radius: 3px;
            border: 1px solid #000000;
        }

        #searchbutton {
            height: 3.5%;
            font-size: 0.9vw;
            -moz-border-radius: 3px;
            -webkit-border-radius: 3px;
            border-radius: 3px;
            background: -moz-linear-gradient(#ff6a00, #ffd800);
            background: -webkit-gradient(linear, 0 0, 0 100%, from(#ff6a00), to(#ffd800));
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff6a00', endColorstr='#ffd800');
            border-color: black;
            width: 150px;
            border: 1px solid #000000;
        }

        #errorwords {
            color: #ff0000;
            font-size: 1.0vw;
        }

        #rightwords {
            color: #00ff21;
            font-size: 1.0vw;
        }
    </style>
</head>
<body>
    <script>
        function getSearchValue() {
            return document.getElementById("searchsentenсe").value;
        }
        function sortSearchValues(value) {
            var searchValue = value;
            if (searchValue.includes("#") && /[A-Za-zА-Яа-яёЁ0-9]+$/.test(searchValue)) {
                document.getElementById("errorwords").style.display = "none";
                document.getElementById("rightwords").style.display = "inline";
                searchValue = searchValue.replace(/[^a-zA-Z0-9#]+/g, '');
                hashtagwords = searchValue.split('#');
                for (var i = 0; i < hashtagwords.length; i++) {
                    if (hashtagwords[i] == '' || hashtagwords[i] == ' ') {
                        hashtagwords.splice(i, 1);
                    }
                }
                return hashtagwords;
            }
            else {
                document.getElementById("rightwords").style.display = "none";
                document.getElementById("errorwords").style.display = "inline";
                return null;
            }
        }
        function mySearch() {
            var hashtagwords = sortSearchValues(getSearchValue());
            if (hashtagwords != null) {
                
            }
        }
        $('#searchsentenсe').on('keyup', function (e) {
            if (e.which === 13) {
                mySearch();
            }
        });
    </script>
    <div class="workspace">
        <h1><nobr>Поиск по хештегам</nobr></h1>
        <form method="post">
            <p>
                <input type="search" id="searchsentenсe" maxlength="70" placeholder="#example" value="I should do the mechanism which works when user clicks Enter" size="130">
                <input type="button" onclick="mySearch()" id="searchbutton" value="Поиск" />
            </p>
        </form>
        <p style="display: none;" id="errorwords">
            Ошибка из-за введённых/невведённых символов! Пожалуйста, введите правильные символы, из которых может состоять хештег!
        </p>
        <p style="display: none;" id="rightwords">
            Всё работает!
        </p>
    </div>
</body>
</html>

In theory, when I press Enter, the mySearch () function should be called, as in the code:
$('#searchsentenсe').on('keyup', function (e) {
            if (e.which === 13) {
                mySearch();
            }
        });

But when I press Enter, the function is not called for me, but it goes to the page with the same url-address, but completely white. Why is that? Why is the method not being called? How to fix it? I just want that when I enter data into the field and press Enter, it will call the function

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
its2easyy, 2020-08-24
@its2easyy

You need to cancel the default form submissione.preventDefault()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question