在使用JQUERY提交之前检查表单输入值时出现问题

problems in checking the form input values before submitting using JQUERY

本文关键字:输入 表单 问题 检查表 检查 JQUERY 提交      更新时间:2023-09-26

我的代码如下

$(document).ready(function() {
    // this is a button to add the form to the DOM tree.
    $("#submitPara").click(function() {
        $("body").append('<form id = "dimensionAndObjects" action = "#"></form>');
        some code to add some input fields, omitted...
        $('#dimensionAndObjects').append('<p><input id = "submitDO" type = "submit" value = "submit"></input></p>');
        return true;
    });
    $('#dimensionAndObjects').submit(function() {
        alert("haahhhahhaha");
        return false;
    });
});

提交功能似乎不起作用,因为没有显示警报信息。如果我把submit函数放在click函数中,它也不起作用。怎么了?我是jQuery的大一新生,提前感谢!

由于表单是动态创建的,因此需要使用事件委托

$(document).on('submit', '#dimensionAndObjects', function() {
        alert("haahhhahhaha");
        return false;
    });
$('#dimensionAndObjects').live('click',function(e) {
e.preventdefault();
        alert("haahhhahhaha");
        return false;// for not submit the form
return true ;// for submit the form
    });