簡體   English   中英

發送帶有表單的javascript函數變量

[英]Send javascript function variable with form

我需要發送帶有表單的javascript函數返回的變量。

<form name="RegForm"  method="post" action="/validate_accountinfo.php" onsubmit="send()">
</form>


function send()
{
   var number = 5;
   return number;
}

在validate_accountinfo.php中,我想返回該函數的值。 這個怎么做?

在表單中放置一個隱藏字段,然后在javascript函數中設置其值。

隱藏字段:

<input type="hidden" id="hdnNumber">

JavaScript:

function send(){
    var number = 5;
    document.getElementById("hdnNumber").value = number;
}

<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 });

添加隱藏的輸入並在發送之前填充它。 確保指定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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM