我正在制作一个webapp,它在扫描条形码并接收文本时会接收文本,这与在使用键盘在文本框中输入任何字母时所提交的文本相同。我尝试过使用此代码,但是它在从文本框更改焦点时而不是在输入值时提交:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Escaneado</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Texto, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Texto, new { htmlAttributes = new { @class = "form-control", @autofocus = "autofocus" } })
@Html.ValidationMessageFor(model => model.Texto, "", new { @class = "text-danger" })
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Cargar escaneados", "LoadScans")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(document).ready(function () {
$('#Texto').change(function () {
$('form').submit();
});
});
</script>
}
我尝试在脚本中使用.trigger
,如下所示:
<script type="text/javascript">
$(document).ready(function () {
$('#Texto').change(function () {
$('form').submit();
});
$('#Texto').trigger("change");
});
</script>
提交,但是提交后重新加载页面时,页面进入无限提交循环
答案 0 :(得分:1)
使用此脚本,信息已提交且不会循环:
<script type="text/javascript">
$(document).ready(function () {
$('#Texto').on('input', function () {
$('form').submit();
});
});
</script>
答案 1 :(得分:0)
Change()
事件。您需要在传递数据时触发事件,例如.trigger("change")