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

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

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

  1. FileUpLoad (input_def) {
  2. input_def.id = this.uid()
  3. const Label = document.createElement('label')
  4. Label.className = 'custom_file_upload'
  5. const Input = document.createElement('input')
  6. Input.type = 'file'
  7. const Input1 = document.createElement('input')
  8. Input1.type = 'text'
  9. Input1.list = 'car'
  10. const DataList = document.createElement('datalist')
  11. DataList.id = 'car'
  12. const Option1 = document.createElement('option')
  13. Option1.textContent = 'Volvo'
  14. DataList.append(Option1)
  15. const Option2 = document.createElement('option')
  16. Option2.textContent = 'Suzuki'
  17. DataList.append(Option2)
  18. Label.append(Input)
  19. Label.append(Input1)
  20. Label.append(DataList)
  21. const Li = document.createElement('i')
  22. Li.innerText = ' Upload Data'
  23. Li.className = "fa fa-cloud-upload"
  24. Label.append(Li)
  25. const row = document.createElement('div')
  26. row.className = 'ui-form-element section-row'
  27. row.id = input_def.id
  28. row.append(Label)
  29. return row

}
如何解决这个错误?

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

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

yyyllmsg

yyyllmsg1#

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

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

pbgvytdp2#

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

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

相关问题