简体   繁体   English

通过javascript填充html表单中的隐藏字段

[英]Filling a hidden field in a html form through javascript

I am trying to send someone an email through a form on my website. 我正在尝试通过我网站上的表格向某人发送电子邮件。 I want to include a part of my webpage into the email. 我想将网页的一部分包含在电子邮件中。

To do this I use a hidden field, which I try to fill using a piece of javascript. 为此,我使用了一个隐藏字段,我尝试使用一段JavaScript来填充该字段。 However due to my lack of knowledge the code doesn't seem to be triggered. 但是由于我的知识不足,似乎没有触发该代码。

<form action="Emailverstuurd.php" method="POST">
<input id="ExcelForm" type="hidden" name="ExcelForm"/>
<input name="email">
<textarea name="body" rows="10" cols="60"> </textarea>
<script type="text/javascript">
$("#send").submit(function() {
    $("#ExcelForm").val($("#myExcelDiv").html());
}
</script>
<input type="submit" name="send" id="send" value="Send Your Message"/>
</p>
</form>

How can I get my javascript code to run? 如何使我的JavaScript代码运行?

You can do it like this: 您可以这样做:

<form action="Emailverstuurd.php" method="POST" onsubmit="submitUpdate()">
<input id="ExcelForm" type="hidden" name="ExcelForm"/>
<input name="email">
<textarea name="body" rows="10" cols="60"> </textarea>
<script type="text/javascript">
    function submitUpdate()
    {
        $("#ExcelForm").val($("#myExcelDiv").html());
    }
</script>
<input type="submit" name="send" id="send" value="Send Your Message"/>
</p>
</form>

Just add an onsubmit handler to your form field give it a function and just put you value update in that function...I don't see #myExcelDiv I'm assuming that is defined somewhere else in your code? 只需在form字段中添加一个onsubmit处理函数,然后为其提供一个函数,然后在该函数中添加value更新...我看不到#myExcelDiv我假设它是在代码的其他地方定义的?

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

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