首页 \ 问答 \ 将当前选定的列表项从bootstrap插件selectpicker传递到JS(Pass currently selected list item from bootstrap plugin selectpicker into JS)

将当前选定的列表项从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 the selectpicker 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
更新时间:2023-09-01 12:09

最满意答案

问题在于范围。

当控制器被实例化时, 私有类成员 _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 ...
  • 只需使用CreateResponse扩展方法: public class UnhandledExceptionAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) { string message = "My custom message"; context.Response = context. ...
  • 摆脱扫描仪缓冲区中的无效输入 }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 ...
  • 问题在于范围。 当控制器被实例化时, 私有类成员 _db将在任何类方法执行之前初始化。 这是一种CLR行为。 因此,操作过滤器不会捕获在构造/初始化控制器对象期间发生的异常,例如无法构造ModelDBContext实例。 一种解决方案是在每个请求上创建和处理ModelDBContext实例,如果您打算优雅地处理(或记录)连接失败(无论是数据库还是后端服务),有些人可能会认为这是正确的方法。 您可能还会发现IServiceLocator和诸如“Unity”或“Ninject”之类的框架非常有用,因此您不会“硬 ...
  • 通常被接受的处理异常的方法是只有在你能做些什么的时候才能处理它们 。 你当然可以处理一个通用的异常,只是为了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 ...
  • 除零和其他“标准”算术错误的分区由运行时或类库处理,它们不知道用户定义的异常。 您只能在自己的代码中使用自己的例外,并在合适的时候将其throw 。 当然,可以捕获类库引发的任何算术异常,并将它们包装到您自己的异常中: try { ... } catch (java.lang.ArithmeticException exc) { throw new MyException("An arithmetic error occurred", exc); } Division by zero and ot ...
  • 异常管理是一个很大的主题,所以我只会触及表面。 从我读过的文章中可以看出“只处理你可以从中恢复的异常”这是什么意思。 如果您不知道如何从特定异常中恢复,那么捕获它通常没有任何意义。 如果它是Web应用程序或服务,Web服务器本身将处理日志记录和恢复。 在某些情况下,您需要捕获异常,以便可以进行常规恢复,例如通过取消或撤消事务。 在这种情况下,可接受的方法是 捕获异常,执行泛型恢复,然后再次抛出异常。 如果我无法处理异常我该怎么办。 让它支持堆栈? 是。 如果我不处理它我如何记录它并提供用户友好的错误消息。 ...
  • HttpContext在ASP.Net Core中包含用于在请求范围内共享数据的IDictionary类型的Items属性。 这正是你需要覆盖你的情况。 这是一个示例实现: public class ApiExceptionAttribute : ExceptionFilterAttribute { public override void OnException(ExceptionContext context) { var items = ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)