简体   繁体   English

不允许发布表单的按钮组

[英]Button group not allowing form to be posted

I have a choice of radio buttons in a form however I am using them as button group.我可以选择表单中的单选按钮,但是我将它们用作按钮组。 However I want to pass the value selected back to the controller.但是我想将选择的值传递回 controller。 But its not posting back at all it just sits there any reason why a btn group would make a form behave in this way.但是它根本没有发回它只是坐在那里有任何理由为什么 btn 组会使表单以这种方式运行。

@using (Html.BeginForm("Create", "Relationships", FormMethod.Post, null)) { 

    <div class="btn-group" data-toggle="buttons">

        <div class="btn-group type-select pull-left top-buffer add-on btn-group-lg" data-toggle="buttons">
            <label class="btn btn-primary">
                <input type="radio" name="RealtionShipType" data-relationshiptype="1" style="display:none" value="1">
                <i class="fa fa-person-booth fa-4x"></i>
            </label>

            <label class="btn btn-primary">
                <input type="radio" name="RealtionShipType" data-relationshiptype="1" value="2">
                <i class="fa fa-ship fa-4x"></i>
            </label>
            <label class="btn btn-primary">
                <input type="radio" name="RealtionShipType" Value="3">
                <i class="fa fa-location-arrow fa-4x"></i>
            </label>
            <label class="btn btn-primary">
                <input type="radio" name="RealtionShipType" data-relationshiptype="1" value="4">
                <i class="fa fa-brain fa-4x"></i>
            </label>
            <label class="btn btn-primary">
                <input type="radio" name="RealtionShipType" data-relationshiptype="1" value="5">
                <i class="fa fa-car fa-4x"></i>
            </label>

            <label class="btn btn-primary">
                <input type="radio" name="RealtionShipType" style="display:none" value="5">
                <i class="fa fa-mobile fa-4x"></i>
            </label>

        </div>
        <input type="submit" class="btn-success" value="Save And Edit">


    </div>
    }

I have included a .net fiddle for it as expierence the same situation there the form simply wont post.我已经为它提供了一个 .net 小提琴作为体验相同的情况,表单根本不会发布。

https://dotnetfiddle.net/FMiprT https://dotnetfiddle.net/FMiprT

The outer button-group div:外部按钮组 div:

<div class="btn-group" data-toggle="buttons">
...
</div>

seems to be redundant, but it is also preventing your "submit" button's click event from firing (see https://getbootstrap.com/docs/4.0/components/buttons/#toggle-states for details).似乎是多余的,但它也阻止了您的“提交”按钮的单击事件触发(有关详细信息,请参阅https://getbootstrap.com/docs/4.0/components/buttons/#toggle-states )。 You don't need to put your submit button in toggle mode, only the radio buttons need this - and you already have another div around those to do that.您不需要将提交按钮置于切换模式,只有单选按钮需要这个 - 而且您已经有另一个 div 围绕这些按钮来执行此操作。

Just remove that div and the button works again.只需删除该 div 按钮即可再次使用。

Demo: https://dotnetfiddle.net/qeRX9I演示: https://dotnetfiddle.net/qeRX9I

Agree with ADyson, it seems that the issue is related to the outer button-group div.同意 ADyson,似乎问题与外部按钮组 div 有关。 After removing it, we could submit the form.删除它后,我们可以提交表单。

If you don't want to remove the outer button-group div, as a workaround,you could consider use the JQuery method to submit the form.如果您不想删除外部按钮组 div,作为一种解决方法,您可以考虑使用 JQuery 方法提交表单。

code like this:像这样的代码:

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { @class="mainform" }))
{

    <div class="btn-group" data-toggle="buttons">

        <div class="btn-group type-select pull-left top-buffer add-on btn-group-lg" data-toggle="buttons">
            ...
        </div>
        <input type="submit" class="btn-success" value="Save And Edit"> 
    </div>
}

JavaScript code: JavaScript 代码:

<script type="text/javascript">
    jQuery(document).ready(function () {
        $(".btn-success").click(function () {
            //find the form and submit
            $(".mainform").submit();
        });
    });
</script>

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

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