[英]How to set input value by JavaScript document.querySelector
Just I want to add input value using javascript. 我想用javascript添加输入值。 Here's priject link !> add to cart > Proceed to Checkout ||
这是priject链接 !>添加到购物车>继续结帐|| then you get the checkout page.
然后你得到结帐页面。
Actually I want to add automatically Zipcode value in the checkout page. 其实我想在结帐页面中自动添加Zipcode值。 I try this methood but dosen't work
我尝试这种方法,但没有工作
document.querySelector('#shipping-new-address-form div:nth-child(8) input').value = '12345';
Can anyone help to solve this issue? 任何人都可以帮忙解决这个问题吗?
You can use chrome console to ensure about your query finding target element and setting its value, exactly because your selector may not be right. 您可以使用chrome控制台来确保您的查询查找目标元素并设置其值,因为您的选择器可能不正确。
It should be like that 它应该是那样的
document.querySelector('input[name = "' + prop + '"]').setAttribute('placeholder', data[prop])
There is another answer like this here QuerySelector set text value 这里有另一个答案,这里QuerySelector设置文本值
on the checkout page, you can define onload
event. 在结帐页面上,您可以定义
onload
事件。
<body onload="fillZipCode()">
<script>
function fillZipCode() {
document.querySelector('#shipping-new-address-form div:nth-child(8) input').value = '12345';
</script>
If you want to add input field and its value using JavaScript then you can use below script. 如果要使用JavaScript添加输入字段及其值,则可以使用以下脚本。
<script>
function myFunction()
{
var input = document.createElement("INPUT");
input.setAttribute("type", "text");
input.setAttribute("value", "1234");
document.body.appendChild(input);
}
myFunction();
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.