イベントがトリガーされた要素に関連する要素を選択できます。
$(".bplus").click(function() {
//find the input relative to this element
$(this).closest('td').prev().children('input').val(function (i, oldValue) {
//make sure the old value is being interpreted as an integer
oldValue = parseInt(oldValue, 10);
//if the old value is a valid integer below 999 then add one,
//otherwise return 999
return (oldValue != NaN && oldValue < 999) ? (oldValue + 1) : 999;
});
});
$(".bminus").click(function() {
//find the input relative to this element
$(this).closest('td').next().children('input').val(function (i, oldValue) {
//make sure the old value is being interpreted as an integer
oldValue = parseInt(oldValue, 10);
//if the old value is a valid integer above 0 then subtract one,
//otherwise return 0
return (oldValue != NaN && oldValue > 0) ? (oldValue - 1) : 0;
});
});
ここにデモがあります:http://jsfiddle.net/uSzr7/16/
ここにあなたのためのいくつかのドキュメントがあります:
検証を処理するもう 1 つの方法は、常に 1 を加算または減算し、その後、値が有効であることを確認するchange
イベント ハンドラーを要素に追加することです。入力タグinput
を使用しているため、これはネイティブ フォーム コントロールの操作に役立ちます。type="number"