gpt4 book ai didi

javascript - 同时将用户输入的文本复制到文本区域

转载 作者:行者123 更新时间:2023-12-02 15:56:45 25 4
gpt4 key购买 nike

嗨,我正在尝试建立一个系统,用户可以输入他们的姓名、地址、邮政编码等,然后系统会生成一封预先写好的信件。我的所有代码目前都在 PHP 文件中。

<h2>Send your MP our pre-written letter</h2>
<p>Please fill out the information required below:</p>
<div align="left">
<form action="form.php" method="post" enctype="multipart/form-data"
<p>Name <font color="red">(required) </font> <input name="flname" required placeholder="Your Name"> </p>
<p>Email <font color="red">(required) </font> <input name="emailfrom" type="email" required placeholder="Your Email"> </p>
<p>Address <font color="red">(required) </font> <input name="address" required placeholder="Address"> </p>
<p>City <font color="red">(required) </font> <input name="city" required placeholder="City"> </p>
What is 2+2? <font color="red">(required) </font> <input name="human"required placeholder="Type Here">
<p> <b> You can edit the text below as much or as little as you wish </b> </p>

<script>

$("input[name=name]").on('keyup', function () {
$('#name1').html($(this).val());
});

$("input[name=address]").on('keyup', function () {
$('#add1').html($(this).val());
});
$("select[name=city]").on('change', function () {
$('#city1').html($(this).find('option:selected').text());
});

</script>

<textarea name="text" required placeholder="Your Message" style="margin: 5px; width: 391px; height: 230px;">
Dear <?php echo "$mptitle $mpsecondname"?>,

Please support us

Kind Regards,
<?php
echo "<p id="name1"></p>"
echo "<p id="add1"></p>" , echo "<p id="city1"></p>"
echo "$senderpostcode"?>
?>

</textarea>

</div>
</div>
</form>

但是,这个 Javascript 解决方案似乎不起作用。我希望文本显示在“亲切的问候”行下,而无需单击提交按钮,即实时显示,因此当用户键入他的姓名/地址/城市时,这些值将按指定的顺序实时显示。

感谢您的帮助。

更新 2:嗨,下面的解决方案似乎只能产生:

可以在 hhassan.me/codetest 上查看表单的实际情况,使用 ST5 3JL 作为演示邮政编码。

下面的图片作为辅助(无法嵌入,因为我是新用户):

[/image/6N48v.png

最佳答案

将 JS 移至页面末尾,这样就不会引用 DOM 尚未加载的元素。使用下面的语法来放置你的 JS 代码,并将其放在页面的末尾..

<?php 
echo "<p id='name1'></p>";
echo "<p id='add1'></p>";
echo "<p id='city1'></p>";
echo "$senderpostcode";
?>
<小时/>
    <script>
$(document).ready(function () {

// put JS code here

});
</script>

</body>
</html>

关于javascript - 同时将用户输入的文本复制到文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31505986/

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