最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use bootstrap form layout without actually using <form>? - Stack Overflow

programmeradmin2浏览0评论

We need the formatting and layout that comes with using something like the following but we don't want the traditional HTML form (we use ajax, javascript to pull/set data on our controls). The problem is when you hit 'enter' the page assumes you are submitting a form, probably because there is form tag (but there was no submit button, so maybe it is defaulting to one of the buttons, but that's a different story):

<form role="form" class="form-horizontal">      
        <legend>Product Header Information</legend>
        <div class="form-group form-group-sm">
            <label for="pid" class="control-label field_name2 col-xs-4 col-sm-4 col-md-3">Product ID:</label>
            <div class="col-xs-8 col-sm-4 col-md-4">
                    <input class="form-control" id="pid" />
            </div>
        </div>
.....
</form>

We need the formatting and layout that comes with using something like the following but we don't want the traditional HTML form (we use ajax, javascript to pull/set data on our controls). The problem is when you hit 'enter' the page assumes you are submitting a form, probably because there is form tag (but there was no submit button, so maybe it is defaulting to one of the buttons, but that's a different story):

<form role="form" class="form-horizontal">      
        <legend>Product Header Information</legend>
        <div class="form-group form-group-sm">
            <label for="pid" class="control-label field_name2 col-xs-4 col-sm-4 col-md-3">Product ID:</label>
            <div class="col-xs-8 col-sm-4 col-md-4">
                    <input class="form-control" id="pid" />
            </div>
        </div>
.....
</form>
Share Improve this question edited Mar 4, 2015 at 14:32 amphetamachine 30.6k12 gold badges67 silver badges74 bronze badges asked Mar 4, 2015 at 13:21 AlpharettaTechyAlpharettaTechy 3902 silver badges15 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 20

Here is the answer, simply replace "form" with "div", that seems to work great in firefox, IE & Chrome. It does makes sense.. I need css classes, I use css classes, who cares on what tag?

<div class="form-horizontal">      
        <legend>Product Header Information</legend>
        <div class="form-group form-group-sm">
            <label for="pid" class="control-label field_name2 col-xs-4 col-sm-4 col-md-3">Product ID:</label>
            <div class="col-xs-8 col-sm-4 col-md-4">
                    <input class="form-control" id="pid" />
            </div>
        </div>
.....
</div>

Using form is the right way. In order to prevent the form from refreshing the page on submit you can use jQuery:

("form").on("submit", function () { /*your ajax*/ return false; });

发布评论

评论列表(0)

  1. 暂无评论