2015-12-21 40 views
0

我创建了一个计数器,当然还有停止和开始按钮。我想做一个简单的时间开始时间,并没有使用很多jQuery我想到的方式来保存它使用PHP只有极少量的jQuery的使用。从Div复制到按钮上的输入框中单击

停止按钮单击我想要div编号被复制并保存到输入文本框中。这是我的代码,请告诉我出错的地方。表兄弟姐妹每当我点击停止它说,在输入文本框不确定

这里的HTML

<div class='buttons'> 
    <button class='btn btn-default' onclick='startTimer()' id='button1'>Start</button> 
    <button class='btn btn-default' onclick='stopTimer()' id='button2'>Stop</button> 
    <div> 
     <form role='form' action='create_delete_edit.php' method='POST'> 
      <div class='dataTable_wrapper timer' id='timer' style='font-size:50pt;'>00:00</div></br> 
      <input type='text' id='timerhide' name='timerhide'> 
      <input type='submit' class='btn btn-default' value='Save Duration' id='btnsaveduration' name='btnsaveduration'> 
      <input type='submit' class='btn btn-default' value='Close Timer' id='btnclosetimer' name='btnclosetimer'> 
     </form> 

现在,这里是为O停止功能

function stopTimer() 
{ 
    w.terminate(); 
    timerStart = true; 
    w = null; 
    var n1 = document.getElementById('timer').innerHTML; 
    var n2 = document.getElementById('timerhide'); 
    n2.value = n1.value; 

} 

我如何把价值N1到N2没有遇到这样的错误?

在此先感谢。

+1

表单元素支持'$(“#timerhide”)。html($(“#timer”)。html())' –

+0

'value'。你不能从innerHtml属性中提取值。 document.getElementById('timerhide')。value = n1; – RomanPerekhrest

+0

你的n1 var,实际上是一个字符串,而不是DOM元素。检查我的答案:-) –

回答

1

首先,你需要得到这样的#timer div的内部文本:

var time = $('#timer').html(); 

然后,更新的隐藏字段的像这样的价值:

$('#timerhide').val(time); 

或者,如果你想单行:

$('#timerhide').val($('#timer').html()); 
1

您可以使用clone要做到这一点,或使用的.html()

//clone 
$("#newDiv").html($("#oldDiv").clone()) 

//html 
$("#newDiv").html($("#oldDiv").html())