2

http://jsbin.com/ezecun/edit#javascript,html

以下にリストされている実際のコードが動的に作成されるため、このように記述する必要があります。jsbin で少し簡略化しました。基本的に、使用できないボックスの値で配列を更新するのに非常に時間がかかります。

ご覧いただきありがとうございます。

コード: php

echo "<label style='float:left'>Comments: </label> <textarea onKeyUp=\"editItemInCart(this.value,'comments',".$itemNum.")\" onChange=\"editItemInCart(this.value,'comments',".$itemNum.")\" >".$cart['comments']."</textarea><br />";

JavaScript

function editItemInCart(newValue,fieldName,itemNum) {
    jQuery.ajax({
        type:"POST",
        url: "editItem.html",
        data: "newvalue=" + newValue + "&fieldname=" + fieldName + "&itemNum=" + itemNum,
    })
    //alert(newValue + fieldName + itemNum);
}
4

2 に答える 2

2

入力されたキーごとに、またはユーザーが入力を終了したときに本当に投稿しますか? ほとんどの人は、1 文字が処理される前に単語全体を入力できます。カウントが必要です。このようなもの:

var count = 0;
function doEditItemInCart(newValue,fieldName,itemNum)
{
    count++;
    setTimeout("editItemInCart('"+newValue+"','"+fieldName+"',"+itemNum+","+count+")",200);
}
function editItemInCart(newValue,fieldName,itemNum,cnt) {
if (count == cnt) {
        count = 0;
        jQuery.ajax({
            type:"POST",
            url: "editItem.html",
            data: "newvalue=" + newValue + "&fieldname=" + fieldName + "&itemNum=" + itemNum,
        })
        //alert(newValue + fieldName + itemNum);
    }
}
于 2011-08-23T21:28:26.997 に答える
0

あなたのコメントに基づいて、あなたはイベントをデバウンスしたいと思うようです。keyupBenAlmanのjQueryスロットル/デバウンスプラグインをお勧めします。

var itemNum = $('#item_num_id').val();
$('#textarea_id').keyup($.debounce(250, editItemInCart(this.value,'comments', itemNum)));

上記のコードは、インラインイベントハンドラーを排除し、マークアップとコードを適切に分離します。

于 2011-08-23T21:33:37.467 に答える