如何解决错误typeerror:无法设置只有getter的#< htmlinputelement>的属性列表?

ddrv8njm  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(414)

我是javascript新手,尝试构建一个文件上传,用户可以上传一个文件,或者从下拉列表中选择一个文件,或者输入一个文件路径。
在我尝试创建一个列表“car”以在输入元素中提供两个示例选择选项之前,一切都很正常。

FileUpLoad (input_def) {

input_def.id = this.uid()

const Label = document.createElement('label')
Label.className = 'custom_file_upload'
const Input = document.createElement('input')
Input.type = 'file'

const Input1 = document.createElement('input')
Input1.type = 'text'
Input1.list = 'car'

const DataList = document.createElement('datalist')
DataList.id = 'car'

const Option1 = document.createElement('option')
Option1.textContent = 'Volvo'
DataList.append(Option1)

const Option2 = document.createElement('option')
Option2.textContent = 'Suzuki'
DataList.append(Option2)

Label.append(Input)
Label.append(Input1)
Label.append(DataList)
const Li = document.createElement('i')
Li.innerText = ' Upload Data'
Li.className = "fa fa-cloud-upload"
Label.append(Li)

const row = document.createElement('div')
row.className = 'ui-form-element section-row'
row.id = input_def.id

row.append(Label)
return row

}
如何解决这个错误?

TypeError: Cannot set property list of #<HTMLInputElement> which has only a getter

我试图遵循这个解决方案。
html选择表单,带有输入自定义值的选项
谢谢

yyyllmsg

yyyllmsg1#

这是因为“list”属性是只读的,因此需要使用setattribute函数来设置/更改它。
试试这个:

const Input1 = document.createElement('input')
Input1.type = 'text'
Input1.setAttribute("list","car")
pbgvytdp

pbgvytdp2#

代替 Input1.list = 'car' 用下面这行,因为 list 属性是只读的,实际上返回对dom元素的引用,您需要使用 setAttribute 设置列表属性。

Input1.setAttribute('list', 'cars');

相关问题