2014-07-08 72 views
-1

我有checkbox哪個onchange調用一個函數並執行一些操作。在函數結束時,我得到2個值。我需要將這些值發送到提交表單的服務器上。我怎樣才能做到這一點??Onchange的複選框調用函數,獲取返回值,然後提交表格

myjsp

<script> 
function fnsimplecalc(){ 
//some calculations... 
var w1=/*some value*/; 
var w2=/*some value*/; 
} 
</script> 
    <form name="test" id= "testid" action="Controller" method="post" > 
    <input type="checkbox" id="checkid" onchange="fnsimplecalc()"> 
    </form> 

我需要的形式提交到服務器後,我從fnsimplecalc()函數獲取值。我該怎麼做呢?

+0

搜索「jquery post」 – VtoCorleone

+0

添加您嘗試過的內容。 – j809

+0

我已經添加了我所做的。不知道如何傳遞值。 – user123

回答

1

你可能在窗體內部有兩個隱藏的字段被你的函數填充。

<form name="test" id= "testid" action="Controller" method="post" > 
    <input type="checkbox" id="checkid" onchange="fnsimplecalc()"> 
    <input type="hidden" id="x" value=""> 
    <input type="hidden id="y" value=""> 
</form> 

那麼你的函數應該填充這個字段

function fnsimplecalc(){ 
    //some calculations... 
    var w1=/*some value*/; 
    var w2=/*some value*/; 

    $('#x').val(w1); 
    $('#y').val(w2); 
} 

當您提交表單,隱藏的字段也將被提交。

+0

太棒了:)它工作:) – user123