[英]Send javascript function variable with form
I need to send a variable returned from a javascript function with a form. 我需要发送带有表单的javascript函数返回的变量。
<form name="RegForm" method="post" action="/validate_accountinfo.php" onsubmit="send()">
</form>
function send()
{
var number = 5;
return number;
}
In the validate_accountinfo.php I want to return the value of the function. 在validate_accountinfo.php中,我想返回该函数的值。 How to do this? 这个怎么做?
Place a hidden field in your form, and set its value in your javascript function. 在表单中放置一个隐藏字段,然后在javascript函数中设置其值。
Hidden field: 隐藏字段:
<input type="hidden" id="hdnNumber">
JavaScript: JavaScript:
function send(){
var number = 5;
document.getElementById("hdnNumber").value = number;
}
Add an <input hidden id="thevalue" name="thevalue" />
to the form, set its value using javascript and then submit the form. 将<input hidden id="thevalue" name="thevalue" />
到表单,使用javascript设置其值,然后提交表单。
<form id="RegForm" name="RegForm" method="post" action="/validate_accountinfo.php" onsubmit="send()">
<input hidden id="thevalue" name="thevalue" />
</form>
<script type="text/javascript">
function send()
{
var number = 5;
return number;
}
document.getElementById('thevalue').value = send();
document.getElementById('RegForm').submit();
</script>
创建一个<input type="hidden" id="field" />
并使用jQuery更新其值。
$("#field").attr({value: YourValue });
Add a hidden input and populate it before sending. 添加隐藏的输入并在发送之前填充它。 Make sure you specify a name=
attribute. 确保指定name=
属性。
<form name="RegForm" method="post" action="/validate_accountinfo.php" onsubmit="send()">
<input type="hidden" name="myvalue"/>
</form>
function send()
{
var number = 5;
// jQuery
$('input[name=myvalue]').val( number )
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.