jQuery .on()方法在Firefox上而不是在Chrome上(jQuery .on() method woking on Firefox but not on Chrome)
我正在尝试在我的网页上创建一个特定的功能,它使用jQuery迭代JSON对象,使用其值填充组合框,并使用jQuery的
.on()
方法将click事件处理程序附加到选项。在我决定在Google Chrome上测试功能之前 ,一切似乎都运行正常。 (到目前为止,我一直在开发Firefox Developer Edition)。 来自jQuery的
.on()
方法似乎根本不适用于谷歌浏览器。这是我的网页的HTML代码段
<div id="showotherinfo"> <div class="form-group" id="existingRedirection" style=""> <label for="serviceNameList">Select a Pretty Redirection to edit it, or add a new one below</label> <select class="form-control" id="serviceNameList" name="serviceNameList"> <option value="0">-Add New-</option> <option value="57">Infibeam Redirection</option> <option value="53">Mozilla Developers Redirection</option> <option value="56">SnapDeal Redirection</option> </select> <hr style="height: 1px; border: none; color: #333; background-color: #333;"> </div> <div class="form-group"> <label for="name">Service Name<font color="red" size=3>*</font></label> <input class="form-control" name="name" id="name"> </div> <div class="form-group"> <label for="serviceUrl">Target URL<font color="red" size=3>*</font></label> <input class="form-control" name="serviceUrl" id="serviceUrl"> </div> <label for="prettyUrl">Pretty URL<font color="red" size=3>*</font></label> <div class="form-inline"> <div class="form-group"> <input type="text" class="form-control" name="prettyUrl" id="prettyUrl" > </div> </div> <div class="form-group" style="margin-top: 19px;"> <label for="redirectType">Redirect Type<font color="red" size=3>*</font></label> <select class="form-control" name="redirectType" id="redirectType"> <option value="0" SELECTED>-Redirection Type-</option> <option value="307">Temporary(307)</option> <option value="301">Permanent(301)</option> <option value="302">Found(302)</option> </select> </div> <div class="checkbox"> <label style="font-weight: bold;"> <input type="checkbox" name="nofollow" id="nofollow"> NoFollow this link </label> </div> <div class="form-group"> <button id="saveRIP" type="button" class="btn btn-primary" onclick="saveData('save')">Save</button> <button id="updateRIP" type="button" class="btn btn-primary" onclick="saveData('update')" >Update</button> <button id="delete" type="button" class="btn btn-danger" onclick="saveData('delete')">Delete</button> <button id="resetRIP" type="button" class="btn btn-danger" onclick="resetValue()" style="margin-left: 45px;">Reset</button> </div> </div>
以下是完成所有工作的jQuery代码
var $select = $('#serviceNameList'); var $firstOption = $("<option/>"); $firstOption.attr("value", 0); $firstOption.text("-Add New-"); $firstOption.on('click', function() { $('#name').val(""); $('#serviceUrl').val(""); $('#prettyUrl').val(""); $('#redirectType').val(""); $('#nofollow').removeAttr('checked'); $('#saveRIP').show(); $('#updateRIP').hide(); $('#delete').hide(); }); $select.append($firstOption); $(jsonResponse).each(function (index, o) { var $option = $("<option/>"); $option.attr("value", o.id); $option.on('click', function() { $('#name').val(o.service_name); $('#serviceUrl').val(o.service_url); $('#prettyUrl').val(o.pretty_url); $('#redirectType').val(o.redirect_type); if (o.no_follow == 1) { $('#nofollow').prop("checked", true); } else { $('#nofollow').prop("checked", false); } $('#saveRIP').hide(); $('#updateRIP').show(); $('#delete').show(); }); $option.text(o.service_name); $select.append($option); }); if ($("#serviceNameList option:selected").val() == 0) { $('#saveRIP').show(); $('#updateRIP').hide(); $('#delete').hide(); }
这里是JSFiddle链接 - http://jsfiddle.net/6uy1quLg/5/ (尝试在Google Chrome和Firefox上运行它以查看差异)
从小提琴中可以明显看出,填充组合框的代码工作得很好, 但是对于附加click事件处理程序的代码似乎根本没有执行。
有人可以帮我解决这个问题吗? 提前致谢。
I am trying to create a specific functionality on my web page, which uses jQuery to iterate over a JSON object, populates a combo box with its values, and also attach click event handlers to the options using jQuery's
.on()
method.All seemed to be working fine, until I decided to test the functionality on Google Chrome. (I had been developing on Firefox Developer Edition till now). The
.on()
method from jQuery does not seem to be working on Google Chrome at all.Here is the HTML code snippet for my web page
<div id="showotherinfo"> <div class="form-group" id="existingRedirection" style=""> <label for="serviceNameList">Select a Pretty Redirection to edit it, or add a new one below</label> <select class="form-control" id="serviceNameList" name="serviceNameList"> <option value="0">-Add New-</option> <option value="57">Infibeam Redirection</option> <option value="53">Mozilla Developers Redirection</option> <option value="56">SnapDeal Redirection</option> </select> <hr style="height: 1px; border: none; color: #333; background-color: #333;"> </div> <div class="form-group"> <label for="name">Service Name<font color="red" size=3>*</font></label> <input class="form-control" name="name" id="name"> </div> <div class="form-group"> <label for="serviceUrl">Target URL<font color="red" size=3>*</font></label> <input class="form-control" name="serviceUrl" id="serviceUrl"> </div> <label for="prettyUrl">Pretty URL<font color="red" size=3>*</font></label> <div class="form-inline"> <div class="form-group"> <input type="text" class="form-control" name="prettyUrl" id="prettyUrl" > </div> </div> <div class="form-group" style="margin-top: 19px;"> <label for="redirectType">Redirect Type<font color="red" size=3>*</font></label> <select class="form-control" name="redirectType" id="redirectType"> <option value="0" SELECTED>-Redirection Type-</option> <option value="307">Temporary(307)</option> <option value="301">Permanent(301)</option> <option value="302">Found(302)</option> </select> </div> <div class="checkbox"> <label style="font-weight: bold;"> <input type="checkbox" name="nofollow" id="nofollow"> NoFollow this link </label> </div> <div class="form-group"> <button id="saveRIP" type="button" class="btn btn-primary" onclick="saveData('save')">Save</button> <button id="updateRIP" type="button" class="btn btn-primary" onclick="saveData('update')" >Update</button> <button id="delete" type="button" class="btn btn-danger" onclick="saveData('delete')">Delete</button> <button id="resetRIP" type="button" class="btn btn-danger" onclick="resetValue()" style="margin-left: 45px;">Reset</button> </div> </div>
And here is the jQuery code that does all the work
var $select = $('#serviceNameList'); var $firstOption = $("<option/>"); $firstOption.attr("value", 0); $firstOption.text("-Add New-"); $firstOption.on('click', function() { $('#name').val(""); $('#serviceUrl').val(""); $('#prettyUrl').val(""); $('#redirectType').val(""); $('#nofollow').removeAttr('checked'); $('#saveRIP').show(); $('#updateRIP').hide(); $('#delete').hide(); }); $select.append($firstOption); $(jsonResponse).each(function (index, o) { var $option = $("<option/>"); $option.attr("value", o.id); $option.on('click', function() { $('#name').val(o.service_name); $('#serviceUrl').val(o.service_url); $('#prettyUrl').val(o.pretty_url); $('#redirectType').val(o.redirect_type); if (o.no_follow == 1) { $('#nofollow').prop("checked", true); } else { $('#nofollow').prop("checked", false); } $('#saveRIP').hide(); $('#updateRIP').show(); $('#delete').show(); }); $option.text(o.service_name); $select.append($option); }); if ($("#serviceNameList option:selected").val() == 0) { $('#saveRIP').show(); $('#updateRIP').hide(); $('#delete').hide(); }
And here is the JSFiddle link- http://jsfiddle.net/6uy1quLg/5/ (Try running it on both Google Chrome and Firefox to see the difference)
As is evident from the fiddle, the code to populate the combo box is working just fine, but to code to attach click event handler doesn't seem to be executing at all.
Can anybody please help me out regarding this?? Thanks in advance.
原文:https://stackoverflow.com/questions/29507364
最满意答案
我正在使用下面的代码,它对我来说工作正常。我也在Web API中使用它来以字符串形式返回View。
对于更多,你可以创建静态类 ,并使下面的方法静态。
public string RenderPartialView(string controllerName, string viewName, object model = null) { System.Web.HttpContextBase contextBase = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current); var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); var controllerContext = new System.Web.Mvc.ControllerContext(contextBase, routeData, new EmptyController()); var razorViewEngine = new System.Web.Mvc.RazorViewEngine(); var razorViewResult = razorViewEngine.FindPartialView(controllerContext, viewName, false); var writer = new StringWriter(); System.Web.Mvc.ViewContext viewContext; viewContext = new System.Web.Mvc.ViewContext(controllerContext, razorViewResult.View, new System.Web.Mvc.ViewDataDictionary(model), new System.Web.Mvc.TempDataDictionary(), writer); viewContext.ViewData["controller"] = controllerName; HttpContext.Current.Items.Add("controller", controllerName); razorViewResult.View.Render(viewContext, writer); string htmlString = writer.ToString(); writer.Dispose(); return htmlString; }
private class EmptyController: System.Web.Mvc.ControllerBase { protected override void ExecuteCore() {} }
现在请找到我们如何从控制器调用它的例子。
RenderPartialView("Home", string.Format("~/Views/Home/{0}.cshtml", "Index"));
我希望这对你有用。
I am using below code and it is working fine for me.i am also using it in Web API for returning View in form of string.
For more you can create Static Class and make below method static.
public string RenderPartialView(string controllerName, string viewName, object model = null) { System.Web.HttpContextBase contextBase = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current); var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); var controllerContext = new System.Web.Mvc.ControllerContext(contextBase, routeData, new EmptyController()); var razorViewEngine = new System.Web.Mvc.RazorViewEngine(); var razorViewResult = razorViewEngine.FindPartialView(controllerContext, viewName, false); var writer = new StringWriter(); System.Web.Mvc.ViewContext viewContext; viewContext = new System.Web.Mvc.ViewContext(controllerContext, razorViewResult.View, new System.Web.Mvc.ViewDataDictionary(model), new System.Web.Mvc.TempDataDictionary(), writer); viewContext.ViewData["controller"] = controllerName; HttpContext.Current.Items.Add("controller", controllerName); razorViewResult.View.Render(viewContext, writer); string htmlString = writer.ToString(); writer.Dispose(); return htmlString; }
private class EmptyController: System.Web.Mvc.ControllerBase { protected override void ExecuteCore() {} }
Now please find example how we can call it from controller.
RenderPartialView("Home", string.Format("~/Views/Home/{0}.cshtml", "Index"));
I hope this will be useful to you.
相关问答
更多-
你可以使用Html.Raw()方法。 You can use the Html.Raw() method for that.
-
这是我想出来的,它对我来说很有用。 我在控制器基类中添加了以下方法。 (您可以随时将这些静态方法置于接受控制器的地方作为参考) MVC2 .ascx风格 protected string RenderViewToString
(string viewPath, T model) { ViewData.Model = model; using (var writer = new StringWriter()) { var view = new WebFormView(ControllerC ... -
Web表单视图引擎集成到MVC中的方式需要控制器上下文,因为它实际上将模板化/呈现到ASP.NET页面类,它将模板内容直接写入响应流。 我建议你看一下spark视图引擎(它会在不改变的情况下渲染WFVE模板)并使用它来从WCF服务中生成模板化的电子邮件。 Spark下载中有这方面的例子。 The way that the web forms view engine is integrated into MVC requires the controller context as it actually bu ...
-
你能通过AJAX在非ASP.Net页面中渲染ASP.Net控件吗?(Can you Render an ASP.Net control in a Non ASP.Net page via AJAX)[2023-05-17]
您可以将控件放在Web服务中,该服务将原始HTML作为输出返回,并从AJAX调用Web服务,使用动态HTML将结果放在任何位置。 您可能必须使用控件的输出缓冲区来使其按照您希望的方式工作。 You could place the control in a web service that returns the raw HTML as output and call the web service from AJAX placing the results wherever you wanted using ... -
唯一可以真正导致此问题的是使用相对URL来引用该文件。 当只加载没有路径的域时,将在URL上正确检索诸如Scripts/foo.js类的引用: http://domain.com/Scripts/foo.js : Scripts/foo.js 。 但是,使用像/Home/Index这样的路径,URL会变成http://domain.com/Home/Index/Scripts/foo.js ,这显然是不正确的。 但是,在这种情况下,您没有使用完全相对路径,因为每个路径都以/为前缀,这应该使它始终从站点根目录 ...
-
我正在使用下面的代码,它对我来说工作正常。我也在Web API中使用它来以字符串形式返回View。 对于更多,你可以创建静态类 ,并使下面的方法静态。 public string RenderPartialView(string controllerName, string viewName, object model = null) { System.Web.HttpContextBase contextBase = new System.Web.HttpContextWrapper(Syst ...
-
ASP.NET MVC中的布局(Layout in ASP.NET MVC)[2022-03-01]
只需包含Layout = null ,您的问题将被解决为: - Index.cshtml: - @{ ViewBag.Title = "Index"; Layout = null; }Index
问题是因为使用'Layout'属性,如果你没有明确指定一个值,将使用_ViewStart.cshtml文件进行布局。 在视图中指定Layout=null将导致它忽略_ViewStart.cshtml文件中指定的布局。 编辑: - 如果你想了解更多关于_ViewStart.cs ... -
ASP.net中的最佳对象布局(Best Object layout in ASP.net)[2021-10-26]
切勿直接暴露字段。 使用属性与私人支持字段。 这使您可以更改实现并封装获取/设置逻辑。 看看Visual Basic团队在这方面要说些什么。 另外,请阅读有关字段和属性之间的差异。 Never expose fields directly. Use properties with private backing fields. This allows you to change implementation and to encapsulate logic around getting/setting th ... -
将ASP.NET控件呈现为HTML字符串将不会呈现选定的事件(Rendering ASP.NET control out to HTML string won't render selected event)[2022-03-12]
将事件处理程序添加到SelectedIndexChanged事件(或任何其他服务器端事件)不会影响呈现DropDownList控件时产生的标记。 事件处理程序仅在服务器上定义和执行,并且在客户端标记中不需要额外的东西。 SelectedIndexChanged事件将在回发时触发,如果列表中所选值自上次呈现之后发生更改。 视图状态用于跟踪之前选择的值,并且来自 -
这应该这样做:
@Html.Raw(Model.HtmlString)
This should do it:@Html.Raw(Model.HtmlString)