2015-02-10 41 views
0

如何使用jQuery訪問多個更新的表單值?使用jquery訪問多個表單值

我有一個jsp形式的文本框顯示保存的值。當這些值更新時,我需要訪問更新的值。 jsp的文本框如下:

<html:text maxlength="11" size="11" property="user.pastAmt" onchange="showInfo();" /> 
<html:text maxlength="11" size="11" property="user.currAmt" onchange="showInfo();" /> 

在showInfo功能,我需要訪問值都user.pastAmt和user.currAmt,無論在哪裏onchange事件是由觸發:

function showInfo() { 
    alert('pastAmt: ' + $("[name='user.pastAmt']").val()); 
    alert('currAmt: ' + $("[name='user.currAmt']").val()); 
} 

問題是,如果我像上面那樣訪問它,我只顯示保存的值,而不顯示更新的值。

我發現我可以發送更新的值中的一個使用「this」關鍵字的事件:

<html:text maxlength="11" size="11" property="user.pastAmt" onchange="showInfo(this);" /> 
<html:text maxlength="11" size="11" property="user.currAmt" /> 

有了:

function showInfo(pastA) { 
    alert('pastAmt: ' + $(pastA).val()); 
    alert('currAmt: ' + $("[name='user.currAmt']").val()); 
} 

但是我怎麼訪問其他?

回答

0

這應該工作,使用jQuery甚至每個偵聽器。如果你可以在一個jQuery選擇器中選擇,你可以將它們結合起來。

$("[name='user.currAmt']").on('change', function() { 
    var result = $("[name='user.currAmt']").val(); 
}); 
$("[name='user.pastAmt']").on('change', function() { 
    var result = $("[name='user.currAmt']").val(); 
}); 
+0

非常感謝!這足以讓我重新開始! – sharcfinz 2015-02-10 23:46:10