简体   繁体   English

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

[英]problems in checking the form input values before submitting using JQUERY

my code is as follows 我的代码如下

$(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;
    });

});

it seems that the submit function doesn't work because the alert info doesn't appear. 似乎由于未出现警报信息,所以提交功能不起作用。 if i put the submit function inside the click function, it doesn't work either. 如果我将提交功能放到点击功能中,它也不起作用。 what's wrong? 怎么了? I am a totally freshman to jQuery, thanks in Advance! 我完全是jQuery的新生,在此先感谢您!

since the form is created dynamically you need to use event delegation 由于表单是动态创建的,因此您需要使用事件委托

$(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
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM