我试图获取正则表达式选择的每个元素的每个值。
具体来说,我有一个像这样的输入列表,由循环生成
<input type="file" name="file[{$some_file.id}]">
anh 我正在尝试通过 jquery 获取每个输入的值,如下所示
$("input[name^='file[']").change(function () {
//get each input value
})
我尝试了 this.val()
但显然它不起作用。我非常感谢您的所有帮助。
最佳答案
事件处理程序 this
绑定(bind)是元素本身,而不是 jQuery
对象。
来自.on()
When jQuery calls a handler, the
this
keyword is a reference to the element where the event is being delivered
所以你想要
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()
关于javascript - JQuery获取由Attribute-Starts-With-Selector选择的元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494510/