表格提交无需提交

时间:2013-12-04 01:31:21

标签: javascript html forms

我有一个没有输入类型='submit'的表单元素。我使用带有onclick的按钮进行函数调用,如下所示:

<form method='post' action='build_meet.html' id='meet_builder_form'>
....
<button onclick='submit_this_form()' >Submit</button>
</form>

该功能定义为:

function submit_this_form() {
    var my_teams = [];
    for ( var i = 0 ; i < window.my_meet.teams.length; i++ ) {
        my_teams.push( window.my_meet.teams[i].team_id );
    }
    var my_teams_str = my_teams.join('|');
    document.getElementById('team_ids_list').value = my_teams_str;
    alert ( my_teams_str );
    //document.getElementById('meet_builder_form').submit();
}

请注意,实际提交表单的行已注释掉。当我单击按钮时,不会生成警报,并且表单仍会提交。

思想?

3 个答案:

答案 0 :(得分:1)

阻止表单的默认操作submit

function submit_this_form(e) {
    e.preventDefault();
    var my_teams = [];
    for ( var i = 0 ; i < window.my_meet.teams.length; i++ ) {
        my_teams.push( window.my_meet.teams[i].team_id );
    }
    var my_teams_str = my_teams.join('|');
    document.getElementById('team_ids_list').value = my_teams_str;
    alert ( my_teams_str );
    //document.getElementById('meet_builder_form').submit();
}

<强> HTML

<form method='post' action='build_meet.html' id='meet_builder_form'>
....
<button onclick='submit_this_meet(event)' >Submit</button>
</form>

答案 1 :(得分:0)

试试这个,

<form method='post' action='build_meet.html' id='meet_builder_form'>
....
<button type="button" onclick='submit_this_form()' >Submit</button>
</form>

答案 2 :(得分:0)

Kinjal是对的。按钮的默认类型(未声明时)是“提交”。您必须声明type="button"以避免按钮提交表单。