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

时间:2018-01-29 04:42:02

标签: javascript jquery jquery-selectors

我试图获取正则表达式选择的每个元素的每个值。

具体来说,我有一个像这样的输入列表,由循环生成

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

我正试图通过像这样的jquery来获取每个输入的值

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

我试过this.val(),但显然它没有用。我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:2)

事件处理程序this绑定是元素本身,而不是jQuery对象。

来自.on()

  

当jQuery调用处理程序时,this关键字是对元素的引用

所以你想要

this.value

请参阅https://developer.mozilla.org/en-US/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">

或者,将元素包装在这样的jQuery对象中并使用.val()方法

$(this).val()
相关问题