您好,我在JavaScript jquery progressbar上遇到问题。错误是该对象不支持属性进度栏。我已经尝试了其他帖子中的所有问题,但都没有问题:(
我还检查了jquery文件是否正确加载。
在底部的Create.cshtml中是进度条的代码。
这是Create.cshtml
@model ContosoTelephonebookContext.TelephoneBook
@{
ViewBag.Title = "Create";
}
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<h2>Create</h2>
@using (Html.BeginForm("Create", "TelephoneBook", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Ad</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@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.Department, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.Department, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Department, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="imageFile">Image file</label>
<div class="col-md-10">
<input type="file" name="imageFile" accept="image/*" class="form-control fileupload" />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.Location, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Mail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Mail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Mail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" onclick="StartInvoicing();" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script>
$( function() {
$( "#progressbar" ).progressbar({
value: 37
});
});
</script>
<div id="progressbar"></div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="../../Scripts/jquery.signalR-2.2.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script type="text/javascript">
$(document).ready(function ()
{
var progressNotifier = $.connection.progressHub;
// client-side sendMessage function that will be called from the server-side
progressNotifier.client.sendMessage = function(message, count) {
// update progress
UpdateProgress(message, count);
alert(count);
};
// establish the connection to the server and start server-side operation
$.connection.hub.start().done(function() {
// call the method CallLongOperation defined in the Hub
progressNotifier.server.getCountAndMessage();
});
});
// Update the progress bar
function UpdateProgress(message, count) {
var result = $("#result");
result.html(message);
$("#progressbar").data("progressbar").value(count);
}
</script>
}
这是_layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Company Telephone Book</title>
@Styles.Render("~/Content/css")
@*@Scripts.Render("~/bundles/modernizr")*@
@Scripts.Render("~/Scripts/jquery-1.12.4.min.js")
@Scripts.Render("~/Scripts/jquery.signalR-2.2.2.min.js")
@Scripts.Render("~/Scripts/jquery-ui-1.12.1.min.js")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Company Telephonebook", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
<li>@Html.ActionLink("TelephoneBook", "Index", "Telephonebook")</li>
<li>@Html.ActionLink("Statistik", "Index", "Statistic")</li>
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - Company telephone book</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
感谢您的支持
现在,我创建了一个mvce,以便更轻松地分析问题。
我创建了一个空的MVC Web项目,仅添加了所需的功能
这是_layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Meine ASP.NET-Anwendung</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/Scripts/jquery-3.2.1.min.js")
@Scripts.Render("~/Scripts/jquery-ui-1.12.1.min.js")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Anwendungsname", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Startseite", "Index", "Home")</li>
<li>@Html.ActionLink("Info", "About", "Home")</li>
<li>@Html.ActionLink("Kontakt", "Contact", "Home")</li>
<li>@Html.ActionLink("test", "Index", "Test")</li>
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - Meine ASP.NET-Anwendung</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
还有test.cshtml
@{
ViewBag.Title = "Test";
}
<div id="progressbar"></div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
$(function() {
$("#progressbar").progressbar({
value: 37
});
});
</script>
}
我再次遇到相同的错误
您好,谢谢您的解决方案。这是完整的解决方案,它现在如何工作。我将正确的版本添加到bundleconfig类中。
using System.Web;
using System.Web.Optimization;
namespace WebApplication1
{
public class BundleConfig
{
// For more information on bundling, visit https://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-3.2.1.min.js",
"~/Scripts/jquery-ui-1.12.1.min.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
// Verwenden Sie die Entwicklungsversion von Modernizr zum Entwickeln und Erweitern Ihrer Kenntnisse. Wenn Sie dann
// ready for production, use the build tool at https://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css",
"~/Content/themes/base/progressbar.css",
"~/Content/themes/base/jquery-ui.min.css"));
}
}
}
最佳答案
问题是加载了两个版本的jQuery。
在此加载一个版本:
@Scripts.Render("~/Scripts/jquery-1.12.4.min.js")
此处加载了另一个版本:
@Scripts.Render("~/bundles/jquery")
一种快速的解决方案是仅将以后的调用替换为您的代码。因此,将第二个示例中的行替换为其他代码(然后将其删除),以使_Layout.cshtml的结尾看起来像这样:
@Scripts.Render("~/Scripts/jquery-3.2.1.min.js")
@Scripts.Render("~/Scripts/jquery-ui-1.12.1.min.js")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
更好的解决方案是实际更改BundleConfig.RegisterBundles中的代码,以便通过捆绑机制将要使用的jquery版本和jquery ui一起加载。
还要注意,要使进度条正常工作,还需要包括jQuery ui一部分的CSS文件。
关于javascript - JavaScript错误:对象不支持属性进度栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48130019/