Php 使用Javascript将值以不同形式插入多个隐藏字段
我有一个HTML表,其中包含从数据库中提取的记录。从MySQL数据库中检索“调用时间”列中的条目。列“Timer”中的条目不是从数据库中检索的,它是一个Javascript计时器 一旦用户单击确认按钮,计时器将停止,并且必须将计时器的最终值插入数据库。下面的Javascript计时器是对其他人的代码稍加修改的版本() 问题:我不知道如何将经过的时间值插入到隐藏的表单字段中。请注意,每个ID为Php 使用Javascript将值以不同形式插入多个隐藏字段,php,javascript,jquery,mysql,Php,Javascript,Jquery,Mysql,我有一个HTML表,其中包含从数据库中提取的记录。从MySQL数据库中检索“调用时间”列中的条目。列“Timer”中的条目不是从数据库中检索的,它是一个Javascript计时器 一旦用户单击确认按钮,计时器将停止,并且必须将计时器的最终值插入数据库。下面的Javascript计时器是对其他人的代码稍加修改的版本() 问题:我不知道如何将经过的时间值插入到隐藏的表单字段中。请注意,每个ID为ID=“stoppedtime的条目都有一个隐藏的表单字段,我知道我必须不断增加stopid变量的值。我
ID=“stoppedtime
的条目都有一个隐藏的表单字段,我知道我必须不断增加stopid变量的值。我需要插入每个经过的时间(在按下确认按钮后)插入到相应的隐藏表单字段中,以便以后可以将这些隐藏表单字段中的值插入到数据库中
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
ElapsedTimeLogger = function(dateElementId, elapsedElementId, interval) {
var container = $(elapsedElementId);
var time = parseDate($(dateElementId).text());
var interval = interval;
var timer;
function parseDate(dateString) {
var date = new Date(dateString);
return date.getTime();
}
function update() {
var systemTime = new Date().getTime();
elapsedTime = systemTime - time;
container.html(prettyPrintTime(Math.floor(elapsedTime / 1000)));
// I Know I have to do something here to put the elapsed time into the hidden field
//But I don't know how exactly to do it
x = document.getElementById("stoppedid"); // The Problem here is that there are Multiple IDs!!
x.value=; prettyPrintTime(Math.floor(elapsedTime / 1000)) // Change the hidden input field value
}
function prettyPrintTime(numSeconds) {
var hours = Math.floor(numSeconds / 3600);
var minutes = Math.floor((numSeconds - (hours * 3600)) / 60);
var seconds = numSeconds - (hours * 3600) - (minutes * 60);
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
var time = hours + ":" + minutes + ":" + seconds;
return time;
}
this.start = function() {
timer = setInterval(function() {update()}, interval * 1000);
}
this.stop = function() {
clearTimeout(timer);
}
}
$(document).ready(function () {
<?php
$count= $totalRows; // Total Number of Entries in the Database
$datevar=1;
$elapsedvar =1;
$timeloggervar= 1;
$confirmvar=1;
if ($count > 0){
do {
$count--;
$timeloggervar++;
$confirmvar++;
$datevar++;
$elapsedvar++;?>
var timeLogger<?php echo $timeloggervar; ?> = new ElapsedTimeLogger("#date<?php echo $datevar; ?>", "#elapsed<?php echo $elapsedvar; ?>", 1);
timeLogger<?php echo $timeloggervar; ?>.start();
$("#Confirm<?php echo $confirmvar; ?>").click(function() { //Stop timer upon clicking the Confirm Button
timeLogger<?php echo $timeloggervar; ?>.stop();
});
<?php } while ($count > 0);}?>
});
</script>
在$(“#确认”)。单击(…
函数,添加以下内容:
var stopvalue = $(this).parents('td').prev().text();
$(this).parents('td').find('input:hidden').val(stopvalue);
调整代码,使其与表格布局配合使用。试试这个
<script>
ElapsedTimeLogger = function(dateElementId, elapsedElementId, hiden, interval) {
.
.
.
function update() {
var systemTime = new Date().getTime();
elapsedTime = systemTime - time;
container.html(prettyPrintTime(Math.floor(elapsedTime / 1000)));
$(hiden).val(prettyPrintTime(Math.floor(elapsedTime / 1000)));
}
.
.
.
$(document).ready(function () {
<?php
for($id=1; $id<$totalRows; $id++){
?>
var timeLogger = new ElapsedTimeLogger("#date<?php echo $id;?>", "#elapsed<?php echo $id;?>","#stoppedid<?php echo $id;?>", 1);
timeLogger.start();
$("#Confirm<?php echo $id; ?>").click(function() { //Stop timer upon clicking the Confirm Button
timeLogger.stop();
});
<?php
}
?>
});
</script>
ElapsedTimeLogger=函数(dateElementId、elapsedElementId、hiden、interval){
.
.
.
函数更新(){
var systemTime=new Date().getTime();
elapsedTime=系统时间-时间;
html(prettyPrintTime(Math.floor(elapsedTime/1000));
$(hiden).val(打印前时间(数学地板(elapsedTime/1000));
}
.
.
.
$(文档).ready(函数(){
var timeLogger=新的ElapsedTimeLogger(“#date”、“#Passed”、“#stoppedid”,1);
timeLogger.start();
$(“#确认”)。单击(函数(){//单击确认按钮后停止计时器
timeLogger.stop();
});
});
那么问题出在哪里?您有一个隐藏的输入字段,它们都有id。只需使用document.getElementById('stoppedtime'+id).value=value\u您想要的;
什么是$datevar
和$elapsedvar
…它们工作正常吗?我认为$stopid
,$datevar
和$elapsedvar
表示相同的值…是不是…???之后只需在按钮上添加一个onclick事件:onclick=“update(correctID)
是的,您现在也必须将ID传递给start函数。$(hiden)->这会为ID为hiden的元素找到正确的ID?元素具有不同的ID,因此我认为这不起作用=)
var stopvalue = $(this).parents('td').prev().text();
$(this).parents('td').find('input:hidden').val(stopvalue);
<script>
ElapsedTimeLogger = function(dateElementId, elapsedElementId, hiden, interval) {
.
.
.
function update() {
var systemTime = new Date().getTime();
elapsedTime = systemTime - time;
container.html(prettyPrintTime(Math.floor(elapsedTime / 1000)));
$(hiden).val(prettyPrintTime(Math.floor(elapsedTime / 1000)));
}
.
.
.
$(document).ready(function () {
<?php
for($id=1; $id<$totalRows; $id++){
?>
var timeLogger = new ElapsedTimeLogger("#date<?php echo $id;?>", "#elapsed<?php echo $id;?>","#stoppedid<?php echo $id;?>", 1);
timeLogger.start();
$("#Confirm<?php echo $id; ?>").click(function() { //Stop timer upon clicking the Confirm Button
timeLogger.stop();
});
<?php
}
?>
});
</script>