我被困在这里:

//Saving Data
    $('#saveButton').click(function(){

        var teamA = $('#teamA').val();
        var teamB = $('#teamB').val();
        var date = $('#date').val();
        var timeOfEvent = $('#timeOfEvent').val();
        var live = "0";
        if($("#live").is(':checked'))
            live = $('#live').val();

        $.ajax({
            url  : "ajax.php",
            type : "POST",
            async: false,
            dataType: "json",
            data :
            {
                'submitAddEvents' : 1,
                'teamA' : teamA,
                'teamB' : teamB,
                'date' : date,
                'timeOfEvent' : timeOfEvent,
                'live' : live,
            },
            success:function(data)
            {

                if(!data.success) {
                    alert(data.message);
                }else{
                    window.location = "addEvents.php";
                }

            }
        });
    });


那就是我已经拥有的。当我单击按钮时,即使在requiredinputs中的selects中,也可以“一切”正常,但是它可以将数据插入数据库中而不会阻止空白输入或选择...

我已经做了什么
我放了<form>并更改了我的ajax

$('#saveButton').click(function(){




$('#saveButton').submit(function(ev){
ev.preventDefault();


并且没有用..在这种情况下,它阻止了空的输入,但是当我填充输入并单击按钮时,什么也没有发生。

最佳答案

您必须选择表单而不是按钮来附加提交侦听器,而不是:

$('#saveButton').submit(function(ev){
ev.preventDefault();


改成:

$('#myform').submit(function(ev){
ev.preventDefault();

10-06 04:23