[英]How to get the key of a multi diamentional select box using jQuery
I'm working on jQuery custom validation of Multidimensional select box. 我正在研究多维选择框的jQuery自定义验证。 I'm trying to access name of the multidimensional select box element:
我正在尝试访问多维选择框元素的名称:
<select id="start_time_195" class="myclass" name="start_time[195]">
Using the following code snippet: 使用以下代码段:
$.validator.addMethod("my_method", function(value, element) {
console.log(element.name);
}, "This text will be displayed as validation message.");
The console output is start_time[195]
控制台输出为
start_time[195]
But I want to get the key 195
out of the element name. 但是我想从元素名称中获取键
195
。 Is there any direct way to get this value? 有没有直接的方法来获得这个价值?
The alternate way I think is to use the .split()
function, but that could be a bit lengthy process. 我认为另一种方法是使用
.split()
函数,但这可能会.split()
一些时间。
Thank you for your help. 谢谢您的帮助。
Add in data like @Rory McCrossan suggested. 添加@Rory McCrossan建议的数据。 You can add id in attribute like data-id = 195
您可以在属性中添加id,例如data-id = 195
$(this).attr('data-id');
If you dont't want to do this then fetch it from the id 如果您不想执行此操作,请从ID中获取
var id =$(this).attr('id'); getid = id.split("_"); getid[2] // this is your value
Try this : 尝试这个 :
$(document).ready(function(){
var name = $("#start_time_195").attr("name");
var patt = /\d+/g;
var value = name.match(patt);
alert(value);
})
Final code : 最终代码:
<html> <head> </head> <body> <select id="start_time_195" class="myclass" name="start_time[195]"></select> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready(function(){ var name = $("#start_time_195").attr("name"); var patt = /\\d+/g; var value = name.match(patt); alert(value); }) </script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.