简体   繁体   English

jQuery获取由Attribute-Starts-With-Selector选择的元素的值

[英]JQuery get value of elements selected by Attribute-Starts-With-Selector

I am trying to get each value of each element selected by regex. 我正在尝试获取正则表达式选择的每个元素的每个值。

Specifically, i have a list of inputs like this, generated by a loop 具体来说,我有一个这样的输入列表,由循环生成

 <input type="file" name="file[{$some_file.id}]">

anh i am trying to get the value of each input by jquery like this 我正在尝试通过jquery来获取每个输入的值

$("input[name^='file[']").change(function () {
  //get each input value
})

I tried this.val() but obviously it did not work. 我尝试了this.val()但显然没有用。 I really appreciate all your help. 非常感谢您的帮助。

The event handler this binding is the element itself, not a jQuery object. this绑定的事件处理程序是元素本身,而不是jQuery对象。

From .on() 来自.on()

When jQuery calls a handler, the this keyword is a reference to the element where the event is being delivered 当jQuery调用处理程序时, this关键字是传递事件的元素引用

so you want 所以你要

this.value

See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Value 参见https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/input/file#Value

 $('input[name^="file["]').on('change', function() { console.info(this.name, this.value) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" name="file[0]"> <input type="file" name="file[1]"> <input type="file" name="file[2]"> <input type="file" name="not-this-one"> 


Alternatively, wrap the element in a jQuery object like this and use the .val() method 或者,将元素包装在这样的jQuery对象中,并使用.val()方法

$(this).val()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM