我想使用相同的脚本获取多个文本框的值。在我的脚本中,我正在获取单个文本框的值更改前后的文本框值。我怎样才能为多个文本框编写相同的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/)这样的框架?框架使事情变得更容易。 –