提交时未输入字段时如何自动滚动到表单区域



我想提交一个表单,但当没有输入字段时,它应该自动指向该区域。我有收音机按钮。当它没有被点击时,它应该自动滚动到单选按钮区域。当我执行代码时,它不是自动滚动到单选按钮,而是自动滚动文本字段。

每当我试图提交表单时,它不是自动滚动的,它只显示错误情况。如何修复此

$('.sellingFormSave').submit(function(e) {
//e.preventDefault();
$('.form_error').remove();
var count = 0;
if (!$('[name=condition]:checked').val()) {
$('.condition_radio .wrapper').after('<label class="form_error" style="color:red">Select the condition of the trucks.</label>');
count++;
}
if ($('[name=product_id]').val() == '') {
var qty = 0;
if (count > 0) {
$('[type=submit]').attr('disabled', false);
return false;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<form method="POST" action="/sellingFormSave" enctype="multipart/form-data" class="sellingFormSave" id="sellingFormSave">
<div class="row">
<div class="form-group col-md-12 condition_radio">
<div class="wrapper">
<span>Condition of the Trucks</span>
<div class="toggle_radio">
<input type="radio" class="toggle_option" id="as_is" value="as_is" name="condition" {{product.condition=='as_is' ? 'checked': ''}} {{product !='' ? 'disabled' : ''}} required>
<input type="radio" class="toggle_option" id="dot_term" value="dot" name="condition" {{product.condition=='dot' ? 'checked': ''}} {{product !='' ? 'disabled' : ''}} required>
<input type="radio" class="toggle_option" id="trade_term" value="trade" name="condition" {{product.condition=='trade' ? 'checked': ''}} {{product !='' ? 'disabled' : ''}} required>
<label for="as_is"><p>As Is - Where Is</p></label>
<label for="dot_term"><p>DOT Term</p></label>
<label for="trade_term"><p>Trade Term</p></label>
<div class="toggle_option_slider">
</div>
</div>
</div>
</div>
<div class="form-group col-md-12">
<div class="field__input-wrapper">
<input type="text" class="field__input" placeholder="Name *" name="name" value="{{product.name}}" {{product !='' ? 'disabled' : 'required'}}>
</div>
</div>
<div class="form-group col-md-4">
<div class="field__input-wrapper">
<input type="text" class="field__input" placeholder="City *" name="city" value="{{product.city}}" {{product !='' ? 'disabled' : 'required'}}>
</div>
</div>
<div class="form-group col-md-12">
<div class="field__input-wrapper field_input__confirm">
<input type="checkbox" id="confirm" name="confirm[]" value="yes" {{product.confirm !='' ? 'checked' : ''}} {{product !='' ? 'disabled' : 'required'}}>
<label for="confirm"> I certify that the above facts are true to the best of my knowledge and belief.</label>
</div>
</div>
</div>
<button type="submit" class="btn">
Save
</button>
</form>
<div class="modal fade bd-example-modal-lg" id="dot_termModal" tabindex="-1" role="dialog" aria-labelledby="dot_termModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5>DOT Term</h5>
<p>sdgfd fdgdfb dfhfdb vchrt </p>
</div>
</div>
</div>
</div>
<!-- dot_term modal -->
<!-- trade_term modal -->
<div class="modal fade bd-example-modal-lg" id="trade_termModal" tabindex="-1" role="dialog" aria-labelledby="trade_termModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5>Trade Term</h5>
<p>xgsd dfhdfjhfdb dhetdjn</p>
</div>
</div>
</div>
</div>
<!-- trade_term modal -->
<!-- as_is modal -->
<div class="modal fade bd-example-modal-lg" id="as_isModal" tabindex="-1" role="dialog" aria-labelledby="as_isModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5>As Is - Where Is</h5>
<p>dfhfdh fdhdfjd.</p>
</div>
</div>
</div>
</div>

  1. 您没有一个名为product_id的字段
  2. 如果选中某个单选框,则忽略其他字段。由于无线电是必需的,所以不能触发";name";或";城市;字段,因为浏览器不允许您在不选择收音机的情况下提交
  3. 什么是product_name,为什么在不使用product_name的情况下将数量设置为0

这是一个演示。我按下保存按钮,这样你就可以看到浏览器会把注意力集中在空名称上。

下次请让代码与HTML 匹配

$('#sellingFormSave').on("submit", function(e) {
$('.form_error').remove();
if (!$('[name=condition]:checked').val()) {
$('.condition_radio .wrapper').after('<label class="form_error" style="color:red">Select the condition of the trucks.</label>');
}
if ($('[name=name]').val() == '') {
e.preventDefault()
$('[name=name]').focus();
}
});
#dummy { margin-bottom: 1000px }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<form method="POST" action="/sellingFormSave" enctype="multipart/form-data" class="sellingFormSave" id="sellingFormSave">
<div class="row">
<div class="form-group col-md-12 condition_radio">
<div class="wrapper">
<span>Condition of the Trucks</span>
<div class="toggle_radio">
<input type="radio" class="toggle_option" id="as_is" value="as_is" name="condition" {{product.condition=='as_is' ? 'checked': ''}} {{product !='' ? 'disabled' : ''}} required>
<input type="radio" class="toggle_option" id="dot_term" value="dot" name="condition" {{product.condition=='dot' ? 'checked': ''}} {{product !='' ? 'disabled' : ''}} required>
<input type="radio" class="toggle_option" id="trade_term" value="trade" name="condition" {{product.condition=='trade' ? 'checked': ''}} {{product !='' ? 'disabled' : ''}} required>
<label for="as_is"><p>As Is - Where Is</p></label>
<label for="dot_term"><p>DOT Term</p></label>
<label for="trade_term"><p>Trade Term</p></label>
<div class="toggle_option_slider">
</div>
</div>
</div>
</div>
<div class="form-group col-md-12">
<div class="field__input-wrapper">
<input type="text" class="field__input" placeholder="Name *" name="name" value="" {{product !='' ? 'disabled' : 'required'}}>
</div>
</div>
<div class="form-group col-md-4">
<div class="field__input-wrapper">
<input type="text" class="field__input" placeholder="City *" name="city" value="" {{product !='' ? 'disabled' : 'required'}}>
</div>
</div>
<div class="form-group col-md-12">
<div class="field__input-wrapper field_input__confirm">
<input type="checkbox" id="confirm" name="confirm[]" value="yes" {{product.confirm !='' ? 'checked' : ''}} {{product !='' ? 'disabled' : 'required'}}>
<label for="confirm"> I certify that the above facts are true to the best of my knowledge and belief.</label>
</div>
</div>
</div>
<hr id="dummy" />
<button type="submit" class="btn">Save</button>
</form>
<div class="modal fade bd-example-modal-lg" id="dot_termModal" tabindex="-1" role="dialog" aria-labelledby="dot_termModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5>DOT Term</h5>
<p>sdgfd fdgdfb dfhfdb vchrt </p>
</div>
</div>
</div>
</div>
<!-- dot_term modal -->
<!-- trade_term modal -->
<div class="modal fade bd-example-modal-lg" id="trade_termModal" tabindex="-1" role="dialog" aria-labelledby="trade_termModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5>Trade Term</h5>
<p>xgsd dfhdfjhfdb dhetdjn</p>
</div>
</div>
</div>
</div>
<!-- trade_term modal -->
<!-- as_is modal -->
<div class="modal fade bd-example-modal-lg" id="as_isModal" tabindex="-1" role="dialog" aria-labelledby="as_isModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5>As Is - Where Is</h5>
<p>dfhfdh fdhdfjd.</p>
</div>
</div>
</div>
</div>

相关内容

  • 没有找到相关文章

最新更新