0

次のスクリプトがPHPページで機能していることをテストしました。

<script>
  function calculate<?=$performance_item_id;?>(){
    alert('<?=$performance_item_id;?>');
    $.post('complete_calculate.php',{
      tid: document.getElementById('performance_item_id<?=$performance_item_id;?>').value
    },
    function(output){
      $('#complete_percentage<?=$performance_item_id;?>').html(output);
    });
  }
  calculate<?=$performance_item_id;?>()
</script>
<span id='complete_percentage<?=$performance_item_id;?>'></span>

まず、postの値をのようなものに変更します"$('.complete<?=$performance_item_id;?>').val();"。つまり、idフィールドから何かを取得するのではなく、同じクラスを持つ複数のフィールドから値を収集します。

<input type = "text" id='complete_percentage<?=$performance_item_id;?>' name = "complete_percentage[]" size = "5" value="0">次に、 " "が機能していないときに、テキストボックスを使用してHTML出力を保持したいと思います。助けてください!ありがとう!

4

1 に答える 1

0

あなたの質問はあまり明確ではありませんが、ここで考慮すべきいくつかのポイントがあります:

  1. PHPとJavaScriptを絶えず混ぜ合わせる必要はありません。
    の値をJavaScript変数に格納してから$performance_item_id、それを再利用します。

  2. 関数を宣言してすぐに呼び出す必要はありません。パターン*を
    使用して、コードをすぐに実行できます。(function(){}())

  3. jQuery.map().get()関数を使用して、すべての値の配列を取得します。

(function () {
    var id = '<?=$performance_item_id;?>';
    alert(id);

    $.post('complete_calculate.php', {   
        tid: $('.complete' + id).map(function(){ return this.value; }).get()
    }, function(output) {
        $('#complete_percentage' + id).html(output);
    }); 
}());

*自己実行型匿名関数または即時呼び出し関数式のいずれかと呼ばれます。


さらに明確にすると、JavaScriptにPHPは必要ないようです

jQuery(function($) {
    $('[class^=complete_]').keyup(function() {
        $.post('complete_calculate.php', {   
            tid: $('.' + this.className).map(function(){ return this.value; }).get()
        }, function(output) {
            $('#complete_percentage' + this.className.replace('complete', '')).html(output);
        }); 
    });
});
于 2012-08-21T03:45:52.263 に答える