2013-03-11 71 views
0

我需要發送一個帶有窗體的javascript函數返回的變量。用表格發送javascript函數變量

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


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

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

+0

請記住給予好評那是有益的所有答案,謝謝。 – redolent 2013-03-12 23:14:34

回答

1

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

隱藏字段:

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

的JavaScript:

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

將表格添加<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> 
0

設爲<input type="hidden" id="field" />並使用jQuery更新它的值。

$("#field").attr({value: YourValue }); 
0

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