0

I'm having problem submitting a HTML form using JavaScript & AJAX from a hyperlink (href) instead of the normal input type. My current code is as follows:

Javascript (executed just before body tag):

$(document).ready(function () {

        //Chronicle Form 1
        $('#fm_chronicledate').submit(function(event) {
            var data = $('#fm_chronicledate').serialize() + 'cron=1' + '&chron_date=' + chron_date.val() + '&chron_time=' + chron_time.val();
                $.ajax({
                    type: "GET",
                    url: "script.php",
                    data: data, 
                    cache: false,
                    success: function(html) {
                        if(html == 1){
                        $('#chron_part1').fadeOut('slow');
                        } else alert('Error');
                    }
                });
            return false;
        });
});

HTML:

<form id="fm_chronicledate" method="POST" action="#" >
<input type="text" class="website" name="chron_time" id="chron_time" />
<input type="text" class="website" name="chron_date" id="chron_date" />
<a href="#" class="chron_submit1" id="chron_submit1" onclick="fm_chronicledate.submit(); return false;">Submit</a>
</form>

Anyways for some reason it just does not execute the AJAX.

Any help would be much appreciated. Thank you.

4

3 回答 3

3

您可以引用主脚本中的链接并捕获点击事件,而不是使用内联 javascript,就像这样......

HTML:

<form id="fm_chronicledate" method="POST" action="#" >
    <input type="text" class="website" name="chron_time" id="chron_time" />
    <input type="text" class="website" name="chron_date" id="chron_date" />
    <a href="#" class="chron_submit1" id="chron_submit1">Submit</a>
</form>

Javascript

$(document).ready(function () {
    $('#chron_submit1').on("click", function(event) {
        var data = $('#fm_chronicledate').serialize() +
            'cron=1' +
            '&chron_date=' + $("#chron_date").val() +
            '&chron_time=' + $("#chron_time").val();
        $.ajax({
            type: "GET",
            url: "script.php",
            data: data, 
            cache: false,
            success: function(html) {
                if (html == 1) {
                    $('#chron_part1').fadeOut('slow');
                }
                else
                    alert('Error');
            }
        });
    });
});
于 2013-09-05T13:12:41.070 回答
1

你为什么不这样做:

<a href="#" class="chron_submit1" id="chron_submit1" onclick="Javascript:GetAjax()">Submit</a>

$(document).ready(function () {
    function GetAjax() {
        var data = $('#fm_chronicledate').serialize() + 'cron=1' + '&chron_date=' + chron_date.val() + '&chron_time=' + chron_time.val();
            $.ajax({
                type: "GET",
                url: "script.php",
                data: data, 
                cache: false,
                success: function(html) {
                    if(html == 1){
                    $('#chron_part1').fadeOut('slow');
                    } else alert('Error');
                }
            });
        return false;
    }
});
于 2013-09-05T13:11:23.650 回答
0

首先从您的链接中删除 onclick 并将其更改为

<form id="fm_chronicledate" method="POST" action="#" >
<input type="text" class="website" name="chron_time" id="chron_time" />
<input type="text" class="website" name="chron_date" id="chron_date" />
<a href="javascript:void(0)" class="chron_submit1" id="chron_submit1">Submit</a>
</form>

将您的活动更改为

$('#chron_submit1').click(函数(事件) {

序列化表单时也有错误。一定是

var data = $('#fm_chronicledate').serialize() + 'cron=1' + '&chron_date=' + $(chron_date).val() + '&chron_time=' + $(chron_time).val();

于 2013-09-05T13:14:37.537 回答