简体   繁体   中英

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

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

I tried this.val() but obviously it did not work. I really appreciate all your help.

The event handler this binding is the element itself, not a jQuery object.

From .on()

When jQuery calls a handler, the this keyword is a reference to the element where the event is being delivered

so you want

this.value

See 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"> 


Alternatively, wrap the element in a jQuery object like this and use the .val() method

$(this).val()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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