PHP中的jQuery表达式
我已经测试了以下脚本是否在我的PHP页面中工作:PHP中的jQuery表达式,php,javascript,jquery,Php,Javascript,Jquery,我已经测试了以下脚本是否在我的PHP页面中工作: <script> function calculate<?=$performance_item_id;?>(){ alert('<?=$performance_item_id;?>'); $.post('complete_calculate.php',{ tid: document.getElementById('performance_item_id<?=$perform
<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_calculate.php'{
tid:document.getElementById('performance\u item\u id')。值
},
功能(输出){
$(“#完成百分比”).html(输出);
});
}
计算()
你的问题不是很清楚,但有几点需要考虑:
无需经常将PHP与JavaScript混合使用。
将$performance\u item\u id
的值存储在JavaScript变量中,然后重新使用该值
无需先声明函数,然后立即调用它。
您可以使用(function(){}())
模式*立即执行代码
使用jQuery的和函数获取所有值的数组
(函数(){
变量id=“”;
警报(id);
$.post('complete_calculate.php',{
tid:$('.complete'+id).map(函数(){returnthis.value;}).get()
},函数(输出){
$('#完成百分比'+id).html(输出);
});
}());
*调用一个自动执行的无注释函数或一个立即调用的函数表达式
进一步澄清后,您的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);
});
});
});
你已经包括了jQuery,但你只在AJAX中使用它…@Blender:你是说它好像是坏的PS:他也在DOM修改中使用它所以,我写的东西可能吗???谢谢它在空载情况下工作!实际上,$('.complete'+id)是整数的文本字段,我希望在onchange时触发该函数。我该如何修改它??谢谢@你能更详细地解释一下吗?当输入
s'值更改时,您希望触发什么?确定。最初,有3组文本字段具有类,即complete1、complete1、complete1、complete2、complete2、complete3、complete3,当我在9个文本字段中的1个字段内输入一个整数并键入时,它将触发您共享的函数。谢谢@火腿-我在我的答案中添加了更多的代码。但是,您确定要在每次键控后进行AJAX调用吗?为什么不使用模糊呢?哦,我发现模糊效果更好!谢谢此外,我希望将complete_calculate.php返回的值显示在文本字段中,而不是显示在span中,是否可能??再次感谢!