Javascript:如何在php中在while循环中编写document.getElementsByCassName()

Javascript:如何在php中在while循环中编写document.getElementsByCassName(),javascript,php,jquery,Javascript,Php,Jquery,如何在php中编写for循环while循环 我有numDifferentiation()javascript函数。我想在中编写这个函数()。我尝试了document.write(),但它替换了整个html页面。如何编写document.getElementsByClassName('price')[]内部while循环 我试过: <?php for($=0;$i<10;$i++){ ?> <script> document.getElementsByClassNa

如何在php中编写for循环while循环

我有
numDifferentiation()
javascript函数。我想在
中编写这个函数()。我尝试了
document.write()
,但它替换了整个html页面。如何编写
document.getElementsByClassName('price')[]内部while循环

我试过:

<?php
for($=0;$i<10;$i++){
?>
<script>
 document.getElementsByClassName('price')[<?php echo $i ?>].innerHTML = numDifferentiation('<?php echo $row['msp']; ?>');
</script>
<?php
}
?>
通过Ajax提交表单

$( document ).ready( function () {
        $( ".searchTerm" ).keyup( function () {
            var text = $( this ).val();

            $.ajax( {
                type: "POST",
                url: "read-project.php",
                data: 'text=' + text,
                dataType: "html",
                async: false,
                success: function ( data ) {
                        if(text == ""){
                            $( '.search_box' ).hide();
                            $( '#project-list-all' ).show();
                            $( '.search_box' ).html("");
                        }


                        else if(text.length >= 2){
                            $( '.search_box' ).show();
                            $( '.search_box' ).html( data );
                        $("#project-list-all:empty").parent().hide();
                        $( '#project-list-all' ).hide();
                        }
                },
                error: function ( errorThrown ) {
                    alert( errorThrown );
                    alert( "There is an error with AJAX!" );
                }

            } );
        } );
    } );
PHP


脚本不应该像那样放在HTML内容中-理想情况下,将它们放在单独的
.js
文件中

请注意,在同一文档中具有相同ID的多个元素是无效的HTML:remove
ID=“price”

使用PHP提供
.price
s数据属性,然后在
.price
s上进行Javascript循环以运行numDifferentiation。例如:

<span class="price" data-msp="<?php echo $row['msp']; ?>">
在页面加载时运行

您可能也应该让您的numDifferentiation变得更好:

function numDifferentiation(val) {
  if(val >= 10000000) return (val/10000000).toFixed(2) + ' Cr';
  else if(val >= 100000) return (val/100000).toFixed(2) + ' Lac';
  else if(val >= 1000) return (val/1000).toFixed(2) + ' K';
  return val;
}

(您不想重新分配参数,只要在找到所需条件时
返回

谢谢您的回答。输出不显示为4.00 lac。它显示为数据库400000生成的HTML是什么样子的?我假设
$row['msp']
将导致一个字符串数据属性,该属性可以隐式转换为一个数字。输出不显示任何内容,当我看到inpect元素时,其显示感谢可能是我的代码错误。我插入了与您给定相同的代码,结果未显示,其他页面在何处。价格值消失,因此我修改了您的代码HTML:现在在窗口中找到加载此代码运行,但当我使用此代码时,ajax显示不起作用
<span class="price" data-msp="<?php echo $row['msp']; ?>">
document.querySelectorAll('.price').forEach((priceSpan) => {
  priceSpan.textContent = numDifferentiation(priceSpan.getAttribute('data-msp'));
});
function numDifferentiation(val) {
  if(val >= 10000000) return (val/10000000).toFixed(2) + ' Cr';
  else if(val >= 100000) return (val/100000).toFixed(2) + ' Lac';
  else if(val >= 1000) return (val/1000).toFixed(2) + ' K';
  return val;
}