0

フォームが送信されたら、たとえば、$。getJsonを使用してサーバーからデータを取得したいと思います。

私はこれを書いた:

$(function () {
    $('#websiteForm').submit(function (event) {
        $.getJSON('Home/checker', function (data) {
            alert(data);
        });
    });
})

私のフォーム:

<form action="" method="post" id="websiteForm">
    <input type="text" name="txtWebsite" id="txtWebsite" />
    <input type="submit" name="submit" />
</form>

しかし、クリックしてフォームを送信しても、何も表示されません。ページの読み込み時に関数をテストしましたが、$.getJSONうまく機能しているので、なぜこの関数が機能しないのかわかりません。

4

2 に答える 2

2

This should make it work

$(function () {
    $('#websiteForm').submit(function (event) {
        $.getJSON('Home/checker', function (data) {
            alert(data);
        });
        return false;
    });
});

without return false; you submit the form (the page reloads) which stops the javascript

于 2012-08-26T22:02:53.353 に答える
2

タイプ送信をボタンで置き換えます。

    $(function () {
        $('button').click(function (event) {
            $.getJSON('Home/checker', function (data) {
                alert(data);
            });
        });
    })

<form action="" method="post" id="websiteForm">
<input type="text" name="txtWebsite" id="txtWebsite" />
<input type="button" name="button" />
</form>

また

$(function () {
        $('#websiteForm').submit(function (event) {
            $.getJSON('Home/checker', function (data) {
                alert(data);
            });
            return false;
        });
    })

<form action="" method="post" id="websiteForm">
<input type="text" name="txtWebsite" id="txtWebsite" />
<input type="submit" name="submit" />
</form>
于 2012-08-26T22:05:58.953 に答える