input修改值的小问题

关于input值修改遇到的坑
正常用jq 修改的时候一般我用

$('input[name=media_file]').val(url)

去修改就ok
然后如果 $(‘input[name=media_file]’) 找出来有多个的话, 就会报错:不能对多个进行操作

Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string

这时候会找出所需要的对象 是第几个去操作

$('input[name=media_file]')[1].val(url)

但是这样会报错:

VM3235:1 Uncaught TypeError: $(...)[1].val is not a function

因为加了[1] 取出来的就是js原生对象。
正常情况 可以用

对象.value = url  

如果是input的话 需要用

$('input[name=media_file]')[1].setAttribute('value', url)

或者再转成jq对象
( ( (‘input[name=media_file]’)[1]).val(url)

猜你喜欢

转载自blog.csdn.net/weixin_43778491/article/details/104377071
今日推荐