簡體   English   中英

jQuery獲取由Attribute-Starts-With-Selector選擇的元素的值

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

我正在嘗試獲取正則表達式選擇的每個元素的每個值。

具體來說,我有一個這樣的輸入列表,由循環生成

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

我正在嘗試通過jquery來獲取每個輸入的值

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

我嘗試了this.val()但顯然沒有用。 非常感謝您的幫助。

this綁定的事件處理程序是元素本身,而不是jQuery對象。

來自.on()

當jQuery調用處理程序時, this關鍵字是傳遞事件的元素引用

所以你要

this.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"> 


或者,將元素包裝在這樣的jQuery對象中,並使用.val()方法

$(this).val()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM