我想使用相同的腳本獲取多個文本框的值。在我的腳本中,我正在獲取單個文本框的值更改前後的文本框值。我怎樣才能爲多個文本框編寫相同的JavaScript?如何獲取多個文本框值?
<div align="center">
<table>
<tr>
<td>
<input type="text" id="bill" name="billid" class="update"
value="<?php echo $get['BillID']; ?>">
</td>
<td>
<input type="text" id="proname" name="productname" class="update"
value="<?php echo $get['Productname']; ?>">
</td>
<td>
<input type="text" id="rate" name="rate" class="update"
value="<?php echo $get['Rate']; ?>">
</td>
<td>
<input type="text" id="quantity" name="quantity" class="update"
value="<?php echo $get['Quantity']; ?>">
</td>
<td>
<input type="text" id="amount" name="amount" class="update"
value="<?php echo $get['Amount']; ?>">
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<input type="submit" name="submit" value="submit" onclick="sendvalue()">
</td>
</tr>
</table>
</div>
我的劇本是
$('#bill').on('focus', function() {
$(this).data('val', $(this).val());
});
function sendvalue() {
var prev = $('#bill').data('val');
var current = $("#bill").val();
//alert(prev+";"+current);
$.ajax({
type: "POST",
url: "updateedit.php",
data: ({
previous: prev,
currentvalue: current,
}),
success: function(data) {
alert(data);
},
error: function() {
alert('failed');
}
});
}
這是一個比較普遍評論:你真的應該看看[接受](https://meta.stackexchange.com/q/5234)你的問題的答案。它有助於未來的訪問者查看是否有答案(以及哪個答案)幫助您解決問題。 –
使用'$(input [type = text])'或'$(「:text」)'選擇器 – inarilo
您是否考慮過像[AngularJS](https://angularjs.org/)這樣的框架?框架使事情變得更容易。 –