将当前选定的列表项从bootstrap插件selectpicker传递到JS(Pass currently selected list item from bootstrap plugin selectpicker into JS)
我是
JS
新手,我正在尝试使用bootstrap中的selectpicker
插件从选择列表中访问选定的值。目前,我的代码为我提供了整个列表,而不仅仅是当前所选的项目。
<div class="form-group"> <select class="selectpicker" name="applicationSelect"> <option value="1">app 1</option> <option value="2">app 2</option> <option value="3">app 3</option> </select> </div>
然后在我的script.js文件中,我有:
var appName = $('select[name=applicationSelect]').val(1).text();
然后,检索到的值将基于
Ajax
事件,但我正在努力寻找一个可行的示例来完成我正在尝试的事情。如何仅检索当前选定的项目?
I'm new to
JS
, I'm trying to access a selected value from a select list using theselectpicker
plugin from bootstrap.Currently my code gives me the entire list rather than just the currently selected item.
<div class="form-group"> <select class="selectpicker" name="applicationSelect"> <option value="1">app 1</option> <option value="2">app 2</option> <option value="3">app 3</option> </select> </div>
Then in my script.js file I have:
var appName = $('select[name=applicationSelect]').val(1).text();
The retrieved value will then be based onto an
Ajax
event, but I'm struggling to find a working example doing what I'm trying to do.How do I retrieve only the currently selected item?
原文:https://stackoverflow.com/questions/35281735
最满意答案
问题在于范围。
当控制器被实例化时, 私有类成员
_db
将在任何类方法执行之前初始化。 这是一种CLR行为。因此,操作过滤器不会捕获在构造/初始化控制器对象期间发生的异常,例如无法构造
ModelDBContext
实例。一种解决方案是在每个请求上创建和处理
ModelDBContext
实例,如果您打算优雅地处理(或记录)连接失败(无论是数据库还是后端服务),有些人可能会认为这是正确的方法。您可能还会发现
IServiceLocator
和诸如“Unity”或“Ninject”之类的框架非常有用,因此您不会“硬编码”new ModelDBContext();
各地的陈述,但这是另一个主题。 根本原因是您在操作方法范围之外进行初始化,操作过滤器将无法捕获它。The problem is scope.
When the controller is instanced the private class member
_db
will initialize before any class methods execute. This is a CLR behavior.Thus, an action filter will not catch an exception incurred during construction/initialization of your controller object, such as a failure to construct a
ModelDBContext
instance.One solution is to create and dispose of
ModelDBContext
instances on every request, and some may argue this is the proper approach if you intend to gracefully handle (or log) connectivity failures (whether it's to a DB or a back-end service.)You may also find
IServiceLocator
and a framework such as 'Unity' or 'Ninject' useful so that you're not "hardcoding"new ModelDBContext();
statements everywhere, but that's another subject. The root cause is you're initializing outside of the scope of an action method, action filters won't catch it.
相关问答
更多-
这是因为你在Debug模式下通过Visual Studio运行它。 如果您在其他地方发布并安装您的应用程序,则只会处理您的全局异常处理程序。 It's because you're running it through Visual Studio in Debug mode. If you release and install your app somewhere else, nothing but your global exception handler will be processed.
-
但我想为我的网络工作提供类似的引擎。 可能吗? 是。 但是,因为网络工作是连续的或者计划的,所以它们在实现方式上存在一些差异。 你可以使用ErrorTrigger来实现你的目标。 一个错误触发器,允许您注释错误发生时由运行时自动调用的函数。 它可以监视web作业执行时的错误。我的演示结果如下: filter null exception 。 有关更多详细信息,请参阅本文 。 在使用Azure WebJob开发作业时,如果执行作业时发生错误,实施错误监视是一种很好的做法。 作为Core扩展的一部分的WebJo ...
-
如何在ExceptionFilterAttribute中返回自定义信息?(How to return custom information in an ExceptionFilterAttribute?)[2023-11-23]
只需使用CreateResponse扩展方法: public class UnhandledExceptionAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) { string message = "My custom message"; context.Response = context. ... -
异常处理(Exceptions handling)[2022-04-26]
摆脱扫描仪缓冲区中的无效输入 }catch(Exception a){ System.out.println("Invalid input please try again"); keyboard.next(); } 代码现在将下降到System.out.println("Again (y/n)"); 如果你想循环回顶部,在keyboard.next();后面加上continue keyboard.next(); Get rid of the invalid input in you ... -
ExceptionFilterAttribute不处理属性异常(ExceptionFilterAttribute not handling properties exceptions)[2022-11-26]
问题在于范围。 当控制器被实例化时, 私有类成员 _db将在任何类方法执行之前初始化。 这是一种CLR行为。 因此,操作过滤器不会捕获在构造/初始化控制器对象期间发生的异常,例如无法构造ModelDBContext实例。 一种解决方案是在每个请求上创建和处理ModelDBContext实例,如果您打算优雅地处理(或记录)连接失败(无论是数据库还是后端服务),有些人可能会认为这是正确的方法。 您可能还会发现IServiceLocator和诸如“Unity”或“Ninject”之类的框架非常有用,因此您不会“硬 ... -
处理异常[closed](Handling Exceptions [closed])[2022-05-30]
通常被接受的处理异常的方法是只有在你能做些什么的时候才能处理它们 。 你当然可以处理一个通用的异常,只是为了log puroposes,但是你应该在完成后重新评估它。 您的应用程序逻辑不应该依赖于例外 。 如果绝对没有其他的方法去做,那么至少要处理具体的例外而不是通用的例外...... The usual accepted way to handle exception is to handle them only if you can do something about it. You can of c ... -
如果您在Swift中设计函数(或方法),则至少有3个选项来处理错误: 选择1:返回可选类型 如果您的函数可能会失败,并且这种情况会定期发生,那么请考虑返回一个可选的类型变量。 例如,在您的情况下,您的方法add可以返回NSDecimalNumber? 而不是普通的NSDecimalNumber 。 在这种情况下,您的方法将检查可能出错的所有内容,并在这些情况下返回nil 。 溢出和下溢将返回nil ,并且所有其他情况将返回NSDecimalNumber 。 调用者必须检查和解包可选的NSDecimalNum ...
-
处理算术异常(Handling Arithmetic Exceptions)[2023-08-18]
除零和其他“标准”算术错误的分区由运行时或类库处理,它们不知道用户定义的异常。 您只能在自己的代码中使用自己的例外,并在合适的时候将其throw 。 当然,可以捕获类库引发的任何算术异常,并将它们包装到您自己的异常中: try { ... } catch (java.lang.ArithmeticException exc) { throw new MyException("An arithmetic error occurred", exc); } Division by zero and ot ... -
处理.net中的异常(handling exceptions in .net)[2022-02-21]
异常管理是一个很大的主题,所以我只会触及表面。 从我读过的文章中可以看出“只处理你可以从中恢复的异常”这是什么意思。 如果您不知道如何从特定异常中恢复,那么捕获它通常没有任何意义。 如果它是Web应用程序或服务,Web服务器本身将处理日志记录和恢复。 在某些情况下,您需要捕获异常,以便可以进行常规恢复,例如通过取消或撤消事务。 在这种情况下,可接受的方法是 捕获异常,执行泛型恢复,然后再次抛出异常。 如果我无法处理异常我该怎么办。 让它支持堆栈? 是。 如果我不处理它我如何记录它并提供用户友好的错误消息。 ... -
ASP.NET核心ExceptionFilterAttribute访问控制器(Access Controller from ExceptionFilterAttribute in ASP.NET Core)[2021-12-20]
HttpContext在ASP.Net Core中包含用于在请求范围内共享数据的IDictionary