简体   繁体   English

使用jquery或ajax更新表后更新输入表单

[英]Updating input form after updating a table using jquery or ajax

I have this table i've built with jquery and ajax (watching some tutorials, so i'm not an advanced programmer). 我有我用jquery和ajax构建的这张表(观看一些教程,所以我不是高级程序员)。 Well, what i do is i insert some values in a table and i need to get all the sum of this values in an input form. 好吧,我要做的是在表中插入一些值,并且需要以输入形式获取所有这些值的和。

I can do that, but what i need is to get the sum without refreshing the page, so anytime i enter: 200 in the Value column, the Sum should become : 我可以做到,但是我需要的是在不刷新页面的情况下获得总和,因此无论何时我在“值”列中输入:200,总和应为:

Sum + 200

Please i need some help with what to do, i've searched for datagrid, but sincerely i don't know how can i do it. 请我在做什么方面需要一些帮助,我已经搜索了datagrid,但是真诚的我不知道我该怎么做。 Thanks 谢谢

Php code of the input : 输入的php代码:

<table class="vlera">

<tr id="<?php echo $id; ?>" class="edit_tr">

<td class="edit_td">
<span id="first_<?php echo $id; ?>" class="text"><?php echo $kodi; ?></span>
<input type="text" value="<?php echo $kodi; ?>" class="editbox" id="first_input_<?php echo $id; ?>" /&gt;
</td>

<td class="edit_td">
<span id="last1_<?php echo $id; ?>" class="text"><?php echo $pershkrimi_pjeses; ?></span>
<input type="text" value="<?php echo $pershkrimi_pjeses; ?>" class="editbox" id="last_input1_<?php echo $id; ?>"/>
</td>
<td class="edit_td">
<span id="last_<?php echo $id; ?>" class="text"><?php echo $vlera; ?></span>
<input type="text" value="<?php echo $vlera; ?>" class="editbox" id="last_input_<?php echo $id; ?>"/>
</td>
<td class="edit_td">
<span id="last2_<?php echo $id; ?>" class="text"><?php echo $kosto; ?></span>
<input type="text" value="<?php echo $kosto; ?>" class="editbox" id="last_input2_<?php echo $id; ?>"/>
</td>

</tr>
<?php
}
?>
</table>

<?php
$sql_shuma="SELECT SUM(vlera) AS shuma FROM servis_pjeset_perdorura WHERE random=$random";
$resultshuma = odbc_exec($connection, $sql_shuma) or die(odbc_error());
while( $rowshuma = odbc_fetch_array($resultshuma ) ) {
           $total1 = $rowshuma['shuma'];
}
?>
<label for='shuma'>Shuma:</label>
<input id="shuma" name="shuma" type="text" value=" <?php echo $total1;?>"  size="20" />

the code you posted doesn't really show the full format (what the inputs look like)... but if you do something like: 您发布的代码并没有真正显示完整格式(输入看起来像)...但是如果您执行以下操作:

$(".values").keyup(function() {
    var sum = 0;
    $(".values").each(function(){
        sum += Number($(this).val());
    });
    $("#shuma").val(sum)
});

and each of your text inputs you want to go towards the total sum has a class of "values" on it, it should work. 并且您想要向总和求值的每个文本输入上都有一类“值”,它应该可以工作。 http://jsfiddle.net/NNbtk/ http://jsfiddle.net/NNbtk/

try this code... This code get the value of the text box when value enter and add it to the sum. 尝试使用此代码...该代码在输入值时获取文本框的值并将其添加到总和中。

$(document).ready(function(){
   $('#shuma').keyup(function(){
   var val=$('#shuma').val();
   sum=sum+val;
    });
});

just put this code inside another php file...say abc.php 只需将这段代码放在另一个php文件中...说abc.php

<?php
$sql_shuma="SELECT SUM(vlera) AS shuma FROM servis_pjeset_perdorura WHERE random=$random";
$resultshuma = odbc_exec($connection, $sql_shuma) or die(odbc_error());

while( $rowshuma = odbc_fetch_array($resultshuma ) ) {
           $total1 = $rowshuma['shuma'];
}
echo $total1;
?>

Then from the main page do the following call on a button lets say button1 然后从主页上对按钮进行以下调用,让我们说出button1

$(document).ready({
     setInterval(function(){            
        $.ajax({
            url: 'abc.php',
            type: "POST",
            success: function(contents){
               $('#shuma').val(contents);
            }

        });
     }, 1000);

});

Now the explanation: 现在说明:
In the main page when document gets ready, setInterval method of javascript will be called which takes 2 parameters, code and delay time in ms. 在文档准备就绪的主页中,将调用javascript的setInterval方法,该方法需要2个参数,代码和延迟时间(以ms为单位)。 this code will call the abc.php after every 1 sec and check the db for the new value and return it and put it in the field. 此代码每隔1秒钟将调用abc.php,并检查db中的新值,然后将其返回并放在字段中。

Hope that was what you were looking for. 希望那是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM