如何解决错误 TypeError: Cannot set property list of #<HTMLInputElement> which has only a getter?

jax*_*jax 2 html javascript css forms

我是 javascript 新手,尝试构建文件上传,用户可以在其中上传文件或从下拉列表中选择文件或输入文件路径。

一切工作正常,直到我尝试创建一个列表“汽车”以在输入元素内提供两个示例选择选项。

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
Run Code Online (Sandbox Code Playgroud)

}

如何解决这个错误?

TypeError: Cannot set property list of #<HTMLInputElement> which has only a getter
Run Code Online (Sandbox Code Playgroud)

我试图遵循这个解决方案。

HTML 选择表单,带有输入自定义值的选项

谢谢

sed*_*uim 5

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

尝试这个:

const Input1 = document.createElement('input')
Input1.type = 'text'
Input1.setAttribute("list","car")
Run Code Online (Sandbox Code Playgroud)