在这个视图中,JQuery不适合我。它会发出警报“它来到这里”和“它来到这里2”而不是$("#PurchaseAmount").val('20');
夹在两个警报(或其他任何东西)之间。有人可以帮忙吗?感谢。
@model StockHoldings.Models.Investments
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js" type='text/javascript'></script>
<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>
</head>
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Investments</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.LastName, (SelectList)ViewBag.LastNames)
@*@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Fund, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Fund, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Fund, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PurchaseDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PurchaseDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PurchaseDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Shares, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.Shares, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.TextBoxFor(model => model.Shares, new { id = "sharesID", name = "Shares", htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Shares, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PurchasePrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.PurchasePrice, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.TextBoxFor(model => model.PurchasePrice, new { id = "purchasepriceID", name = "PurchasePrice", htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PurchasePrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PurchaseAmount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.PurchaseAmount, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.TextBoxFor(model => model.PurchaseAmount, new { id = "purchaseamountID", name = "PurchaseAmount", htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PurchaseAmount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(document).ready(function() {
alert('It got here');
$("#PurchaseAmount").val('20');
alert("It got here 2");
//$("#PurchasePrice").blur(function () {
// alert("Handler for .blur() called.");
//});
$("#Shares,#PurchasePrice").keyup(function (e) {
var q = $("#Shares").val();
alert('q' + q);
var p = $("#PurchasePrice").val();
alert('p' + p);
if (q && p) {
if (!isNaN(q) && !isNaN(p)) {
result = parseFloat(q) * parseFloat(p);
alert('result ' + result);
}
}
$("#PurchaseAmount").val(result);
});
});
</script>
}
再次感谢您的帮助。