简体   繁体   English

如何通过JavaScript document.querySelector设置输入值

[英]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.

相关问题 Javascript document.querySelector 只需要这个值 - Javascript document.querySelector just need the value document.querySelector('input[name=nameOfradio]:checked').value; 但没有检查收音机 - document.querySelector('input[name=nameOfradio]:checked').value; but radio not checked 使用javascript中的document.querySelector在数组中获取值时错误NULL? - error NULL when get value in array with document.querySelector in javascript? 如何实现document.querySelector? - How is document.querySelector implemented? 如何使用本机JavaScript编码document.querySelector(&#39;。class&#39;)。doSomeThing() - How to code document.querySelector('.class').doSomeThing() with native JavaScript 如何将 JavaScript 代码 document.querySelector 转换成 Angular9? - how to convert JavaScript code document.querySelector into Angular9? JavaScript - 为什么当“Document.querySelector().value”在 function 之外时我的代码不运行 - JavaScript - Why doesn't my code run when "Document.querySelector().value" is outside function + how do I get function to run on input number change? document.querySelector.value 返回值 - document.querySelector .value return value 获取 JavaScript 中现有 document.querySelector 的参数? - Get the argument of an existing document.querySelector in JavaScript? 在 JavaScript 中是否有重命名 document.querySelector 等的约定? - Is there a convention for renaming document.querySelector etc in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM