JQuery 获取由 Attribute-Starts-With-Selector 选择的元素的值
JQuery get value of elements selected by Attribute-Starts-With-Selector
我正在尝试获取由正则表达式选择的每个元素的每个值。
具体来说,我有一个这样的输入列表,由循环生成
<input type="file" name="file[{$some_file.id}]">
anh 我正在尝试通过 jquery 像这样
获取每个输入的值
$("input[name^='file[']").change(function () {
//get each input value
})
我试过this.val()
,但显然没有用。非常感谢您的帮助。
事件处理程序 this
绑定是元素本身,而不是 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()
我正在尝试获取由正则表达式选择的每个元素的每个值。
具体来说,我有一个这样的输入列表,由循环生成
<input type="file" name="file[{$some_file.id}]">
anh 我正在尝试通过 jquery 像这样
获取每个输入的值$("input[name^='file[']").change(function () {
//get each input value
})
我试过this.val()
,但显然没有用。非常感谢您的帮助。
事件处理程序 this
绑定是元素本身,而不是 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()