gpt4 book ai didi

javascript - QuerySelector 设置文本值

转载 作者:行者123 更新时间:2023-11-29 10:09:42 29 4
gpt4 key购买 nike

我有这个循环应该设置 <input type="text"/>值为无并更新占位符。当我登录节点时它工作正常但是 valueplaceholder没有更新?怎么了?

数据是一个 JSON 对象。

var data = {"password":"password","username":"xhinii"};

JS :

var data = {"password":"password","username":"xhinii"};

for(var prop in data) {
console.log(document.querySelector('input[name = "' + prop + '"]')); //works fine. logs the node.
document.querySelector('input[name = "' + prop + '"]').value = ''; //doesn't work
document.querySelector('input[name = "' + prop + '"]').setAttribute('placeholder', data[prop]);//doesn't work
}

最佳答案

你不应该解析一个对象只是使用它:

var data= {"password":"password","username":"xhinii"};

for(var prop in data) {
document.querySelector('input[name = "' + prop + '"]').value = prop;
document.querySelector('input[name = "' + prop + '"]').setAttribute('placeholder', data[prop])
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name='password' />
<input name='username' />

关于javascript - QuerySelector 设置文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35672566/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com