简体   繁体   English

使用 javascript 向现有表单字段添加值

[英]Add value to existing form field with javascript

i am trying to add the output of the following code (on page load)我正在尝试添加以下代码的 output(在页面加载时)

<?php the_field('object_id'); ?>

to an field of an form with the id="sf_objekt".到 id="sf_objekt" 的表单字段。 The field has an optional set value "ID".该字段具有可选的设置值“ID”。 The output of the code should replace the "ID".代码的 output 应替换“ID”。

I tried using "append", but with no luck.我尝试使用“追加”,但没有运气。

Any ideas?有任何想法吗? Ty!泰!

If I have correctly undestood your question, the following snippet should do the trick:如果我正确地理解了您的问题,那么以下代码段应该可以解决问题:

 document.querySelector('#sf_objekt').value = "<?php the_field('object_id'); ?>"
 <input id="sf_objekt" type="text" />

Clearly the <?php> tag will be replaced with the output of the PHP function the_field() only if the page is ran through a PHP server. Clearly the <?php> tag will be replaced with the output of the PHP function the_field() only if the page is ran through a PHP server.

This code will auto fill in a form with the result of the php script.此代码将使用 php 脚本的结果自动填写表格。

<html>
<body>
    <form>
      <input type="text" id="sf_objekt">
    </form>
<script>
    document.getElementById("sf_objekt").defaultValue= "<?php echo the_field('object_id'); ?>";
</script>

</body>
</html>

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

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