[英]why jquery can't do submit function?
I can't use jquery for submit the form?! 我无法使用jquery提交表单吗? I want to submit the form without button submit type, but when i click on ok for confirmation submit doesn't work.
我想提交没有按钮提交类型的表单,但是当我单击确定进行确认时,提交不起作用。
@model WebApplication2.Models.Product
<div class="well">
<h3>
<strong>@Model.Name</strong>
</h3>
<button id="@Model.ProductID" class="btn btn-success btn-sm pull-right">
Use
</button>
<span class="lead">
<br />
@Model.NumberLine
</span>
@using (Html.BeginForm("AddToUserProducts", "Home", new { ReturnUrl = ViewContext.HttpContext.Request.QueryString != null ? $"{ViewContext.HttpContext.Request.Path}{ViewContext.HttpContext.Request.QueryString}" : ViewContext.HttpContext.Request.Path.ToString() }, FormMethod.Post, new { id = Model.ProductID, role = "form" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.ProductID)
}
</div>
<script type="text/javascript"> $(document).ready(function () { $("button#@Model.ProductID").click(function () { var temp = Number($("#userNumberLimit").val()) + @(Convert.ToInt32(Model.NumberLine)); if (temp <= 1000000) { if (confirm("Are You Sure?\\nThis will be evaluated in your daily limitation.")) { $("form#@Model.ProductID").submit(); } } else { alert('You have reached daily limitation.'); } }); }); </script>
You need to store the c# model value inside the javascript variable before using it in the script. 您需要先将c#模型值存储在javascript变量中,然后才能在脚本中使用它。
var model = @Html.Raw(Json.Encode(Model));
<script type="text/javascript">
$(document).ready(function () {
$("button#"+model.ProductID).click(function () {
var temp = Number($("#userNumberLimit").val()) + @(Convert.ToInt32(Model.NumberLine));
if (temp <= 1000000)
{
if (confirm("Are You Sure?\nThis will be evaluated in your daily limitation."))
{
$("form#"+model.ProductID).submit();
}
}
else
{
alert('You have reached daily limitation.');
}
});
});
</script>
JQuery could not find the element with a selector: form#123
. jQuery找不到带有选择器的元素:
form#123
。
From jquery document: 从jQuery文档:
Description: Selects a single element with the given id attribute.
描述:选择具有给定id属性的单个元素。
version added: 1.0jQuery( "#id" ) id: An ID to search for, specified via the id attribute of an element.
添加的版本:1.0jQuery(“ #id”)id:要搜索的ID,通过元素的id属性指定。
For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient.
对于id选择器,jQuery使用JavaScript函数document.getElementById(),这非常高效。 When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.
当另一个选择器附加到id选择器(例如h2#pageTitle)时,jQuery在将元素标识为匹配项之前执行附加检查。
For more info about jQuqery ID selector, please reference: 有关jQuqery ID选择器的更多信息,请参考:
https://api.jquery.com/id-selector/ https://api.jquery.com/id-selector/
For the selector $('form#123')
: 对于选择器
$('form#123')
:
JQuery will first find the element with Id: 123
. jQuery将首先找到ID为
123
的元素。 Then it will check if it is a form
. 然后它将检查它是否为
form
。
Now before the form
, there is another element with Id: 123
, and it is a button. 现在在
form
之前,还有另一个带有ID的元素: 123
,它是一个按钮。 jQuery don't think that the result matches your selector. jQuery认为结果与您的选择器不匹配。
<button id="123" class="btn btn-success btn-sm pull-right">
Use
</button>
<form id="123">
...
</form>
So jQuery can not find your form. 因此,jQuery无法找到您的表单。
Chang your form Id from Model.ProductID
to "myForm" + Model.ProductID
. 将您的表单ID从
Model.ProductID
为"myForm" + Model.ProductID
。
Which means you need to change your code to: 这意味着您需要将代码更改为:
new { id = "myform" + Model.ProductID, role = "form" }
$("form#myform@Model.ProductID").submit();
<script type="text/javascript">
$(document).ready(function () {
$("#ProductID").click(function () {
var temp = Number($("#userNumberLimit").val()) + @(Convert.ToInt32(Model.NumberLine));
if (temp <= 1000000)
{
if (confirm("Are You Sure?\nThis will be evaluated in your daily limitation."))
{
$("#@Model.ProductID").submit();
}
}
else
{
alert('You have reached daily limitation.');
}
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.