如果字段为空bootstrap,如何禁用发送按钮 [英] how to disable a send button if fields empty bootstrap

查看:89
本文介绍了如果字段为空bootstrap,如何禁用发送按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Bootstrap制作网页。我有一个简单的代码,有1个输入和1个按钮。当你按下按钮时,其中包含的输入被存储在数据库中。但如果输入为空也将其保存到数据库。我可以使用JavaScript进行验证,但是我想知道引导程序是否有选项来验证这一点。例如引导中的表单验证它,但我不想使用表单。

i am using Bootstrap to make a webpage. I have a simple code with 1 input and 1 button. when you press the button, which contains the input is stored in the database. but if the input is empty also saves it to the database. I can do the validation with javascript but i want to know if bootstrap have an option to validate this. For example forms in bootstrap validate it, but i dont want to use a form.

<div class="row">
    <div class="col-md-4 col-md-offset-1">
        <div class="input-group input-group-lg"> <span class="input-group-addon"><span class="glyphicon glyphicon-fire"></span> </span>
            <input type="text" class="form-control" placeholder="Nombre Marca" id="marca" required>
        </div>
    </div>
</div>
<div class="row">
    <br />
    <div class="col-md-3 col-md-offset-1" align="center" onClick="guardar_marca()"> <a href="#" class="btn btn-primary" role="button">Guardar Marca</a>

    </div>
</div>


推荐答案

我不确定您是否复制了一个小你的HTML部分,但在你的代码片段中,你有一个额外的结束< / div> 。您可以使用jQuery来测试输入值是否为空:

I'm not sure if you just copied a small portion of your HTML, but in your snippet you have an extra closing </div>. You can use jQuery to test if the input value is empty:

$('.btn').click(function(e) {
  if ($('input').val() === '') {
    e.preventDefault();
    alert('input is empty');
  }
});

Bootply

这篇关于如果字段为空bootstrap,如何禁用发送按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆