简体   繁体   English

Bootstrap 4:验证会破坏嵌入式表单元素的样式吗?

[英]Bootstrap 4: validation breaks styling on inline form elements?

I am trying to validate an inline form field, with Bootstrap 4. 我正在尝试使用Bootstrap 4验证内联表单字段。

When validation is triggered, the inline form field jumps in width. 触发验证后,内联表单字段的宽度会跳跃。 Here is a fiddle (just click Enter without entering a value, to see the problem): https://jsfiddle.net/aq9Laaew/213418/ 这是一个小提琴(只需单击Enter而不输入值,即可查看问题): https : //jsfiddle.net/aq9Laaew/213418/

How can I fix this? 我怎样才能解决这个问题?

This is my HTML: 这是我的HTML:

<form class="form-inline needs-validation" id="form-stage1" novalidate>
  <label class="sr-only" for="stage1-amount">Amount in thousands</label>
  <div class="input-group mb-1 mr-sm-1">
    <div class="input-group-prepend"><div class="input-group-text">£</div></div>
    <input type="number" step="5000" min="0" class="form-control" id="stage1-amount" required>
  <div class="invalid-feedback">Please enter an amount to proceed</div>
  </div>
  <button type="submit" class="btn btn-secondary mb-1">Enter</button>
</div>
</form>

And this is my JavaScript: 这是我的JavaScript:

d3.select('#form-stage1').on('submit', function(e) { 
  d3.event.preventDefault();
  d3.event.stopPropagation();
  d3.select(this).attr('class', 'was-validated');
});

Try this : 尝试这个 :

  var form = document.getElementById('form-stage1'); d3.select('#form-stage1').on('submit', function(e) { d3.event.preventDefault(); d3.event.stopPropagation(); d3.select(this).attr('class', 'form-inline was-validated'); $(".invalid-feedback").show(); if (form.checkValidity() !== false) { d3.select('#stage1-results').style('display', 'inline-block'); var val = d3.select('#stage1-amount').property("value"); d3.select('#stage1-output').text(formatPounds(val)); var percentile = getPercentile('property', val) d3.select('#stage1-lower').text(percentile); d3.select('#stage1-higher').text(100-percentile-1); } }); 
 .row { background: #f8f9fa; margin-top: 20px; } .col { border: solid 1px #6c757d; padding: 10px; } .form-control{ width:auto!important; flex:0!important; } .input-group{ width:auto!important; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> <!-- Bootstrap docs: https://getbootstrap.com/docs --> <div class="container"> <form class="form-inline needs-validation" id="form-stage1" novalidate> <label class="sr-only" for="stage1-amount">Amount in thousands</label> <div class="input-group mb-1 mr-sm-1"> <div class="input-group-prepend"><div class="input-group-text">£</div></div> <input type="number" step="5000" min="0" class="form-control" id="stage1-amount" required> </div> <button type="submit" class="btn btn-secondary mb-1">Enter</button> <div class="invalid-feedback">Please enter an amount to proceed</div> </div> </form> </div> 

https://jsfiddle.net/aq9Laaew/215767/ https://jsfiddle.net/aq9Laaew/215767/

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

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