简体   繁体   English

无法使用Javascript更改输入标签的属性

[英]Can't change attribute of input tag using Javascript

I have an <input> tag which I would like to change programmatically. 我有一个<input>标记,我想以编程方式进行更改。 The input don't have inner text, it save its values on aria attributes. 输入没有内部文本,它将其值保存在aria属性上。

So, here is the <input> : 因此,这是<input>

<input type="text" id="foo" aria-valuemin="1" aria-valuemax="10" autocomplete="off" role="spinbutton" aria-valuenow="1">

Consider that we save the tag to the following bla variable: 考虑我们将标签保存到以下bla变量中:

var bla = document.getElementById("foo");

Below is a list of commands I tried using. 以下是我尝试使用的命令列表。 But none of them worked. 但是他们都不起作用。

  • bla.value = 2;
  • bla.value = "2";
  • bla.setAttribute("aria-valuenow",2);
  • bla.setAttribute("aria-valuenow","2");

Any ideas why? 有什么想法吗? The id of the element is unique, I checked it. 元素的id是唯一的,我检查了一下。 I am working on somebody's else code. 我正在处理其他人的代码。 Could it be possible that other developers changed the behavior of the <input> ? 其他开发人员是否可能会更改<input>的行为?

As pointed out my other users, please use unique Id while naming the id of an element. 正如指出我的其他用户,请使用唯一的ID,而命名id的元素。 Below is the solution to your problem using setAttribue() . 以下是使用setAttribue()解决问题的方法。

 var input = document.getElementById("input"); input.setAttribute("aria-valuenow",2); // To set attribute to input element console.log(input.hasAttribute('aria-valuenow')) // To check if input has the mentioned attribute console.log(input.getAttribute('aria-valuenow')); // To access the value of the attribute 
 <input type="text" id="input" aria-valuemin="1" aria-valuemax="10" autocomplete="off" role="spinbutton" aria-valuenow="2"> 

您可以使用,

document.getElementById("mytext").value = "My value";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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