0

フィールドをインクリメントおよびデクリメントする簡単な コードがあります。すべて問題ありませんでしたが、取得するフィールドの数がわかりません (mvc によって生成されます)。私の質問は、ページ上の各フィールドに 2 つのボタンをスマートにバインドする方法です。私は使用しようとしまし $(this.id+"x").val(currentVal - 1)たが、それは間違った方法だと思います。

提案をありがとう

追加:

  1. Jquery Mobile の範囲入力が使えません。
  2. テキスト ボックス "Text Pin #4" は常にフォーカスされている必要があります。
  3. モバイル デバイスでは、すべてのボタンをクリックできる必要があります。
4

2 に答える 2

1

イベントがトリガーされた要素に関連する要素を選択できます。

$(".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"

于 2012-04-23T18:20:55.420 に答える
0
$(".bplus").click(function(){
    var txtField = $(this).parents('tr').find('input[type="number"]')
    var currentVal = parseInt(txt.val());
    if (currentVal < 999)
        txtField.val((currentVal || 0) + 1);
});

I put (currentVal || 0)、つまり の場合currentVal==NaN、0 に置き換えられます

于 2012-04-23T17:34:19.873 に答える