AngularJS:表单中的所有按钮都提交了吗?

时间:2022-11-24 20:11:30

I apologize in advance if this is covered in the docs, but I can't seem to find it.

如果在文档中有,我提前向您道歉,但是我好像找不到。

I have a pretty straightforward form which uses several buttons as I'm using twitter bootstrap. The problem is that clicking any of the buttons in the form seems to trigger a submit event to angular: In my form there are several input fields in which I use the "require" attribute and so clicking any button opens a dialog saying the field is required. This is all well and good, except I only want the validation to take place when the user clicks the actual submit button.

我有一个非常简单的表单,在使用twitter引导时使用了几个按钮。问题是,单击表单中的任何一个按钮似乎都会触发一个提交事件到角度:在我的表单中,有几个输入字段,我在其中使用“require”属性,因此单击任何按钮都会打开一个对话框,表明该字段是必需的。这一切都很好,除了我只希望在用户单击实际的submit按钮时进行验证。

I have tried setting the ng-submit to a function which so far only returns false, but this did not seem to have any effect.

我尝试将ng提交到一个只返回false的函数,但这似乎没有任何效果。

Update: I've found a temporary workaround using a directive which uses event.preventDefault(). However this seems a bit excessive and also means I have to attach it to every button.

更新:我找到了一个使用event.preventDefault()的指令的临时解决方案。然而,这似乎有点过分,也意味着我必须把它附加到每个按钮上。

<div class="row-fluid">
    <div class="span12">
        <form ng-submit="onSubmit()" class="form-horizontal">
            <div class="row-fluid">
                <div class="span12">
                    <div class="katana-technician-form">
                        <div class="control-group">
                            <label class="control-label">Tekniker
                                <div class="controls">
                                    <div class="input-append">
                                        <input type="text" class="span2" />
                                        <div class="btn-group">
                                            <!-- Triggers onSubmit-->
                                            <button data-toggle="dropdown" class="btn dropdown-toggle">Velg<span class="caret"></span>

                                            </button>
                                            <ul class="dropdown-menu">
                                                <li>Kake</li>
                                                <li>Bake</li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row-fluid">
                <div class="span12">
                    <!-- Triggers onSubmit-->
                    <button type="submit"></button>
                </div>
            </div>
    </div>
    </form>
</div>
</div>

1 个解决方案

#1


86  

You should try setting type="button" on the buttons.

您应该尝试在按钮上设置type="button"。

This question goes into detail as to what this does.

这个问题涉及到这个问题的具体内容。

#1


86  

You should try setting type="button" on the buttons.

您应该尝试在按钮上设置type="button"。

This question goes into detail as to what this does.

这个问题涉及到这个问题的具体内容。