2013-06-26 28 views
-1

有一個jQuery代碼:如何在jquery val(val)中回顯文本?

(function($) { 
$.fn.countdown = function(options) { 
    var settings = { 'date': null }; 
    if(options) { 
     $.extend(settings, options); 
    } 

    this_sel = $(this); 

    function count_exec() { 
     eventDate = Date.parse(settings['date'])/1000; // Parse the date string 
     currentDate = Math.floor($.now()/1000);     // Find the timestamp for now 
     seconds = eventDate - currentDate;     // Find the number of seconds remaining 

     days =   Math.floor(seconds/(60 * 60 * 24));  // Divide to find the number of days remaining 
     seconds -=  days * 60 * 60 * 24;      // Subtract the number of (complete, => 24 hours) days calculated above 

     hours =   Math.floor(seconds/(60 * 60));   // Get the number of hours from that modified number^
     seconds -=  hours * 60 * 60; 

     minutes =  Math.floor(seconds/60); 
     seconds -=  minutes * 60; 

     this_sel.find('#days').val(days).trigger('change'); 
     this_sel.find('#hours').val(hours).trigger('change'); 
     this_sel.find('#mins').val(minutes).trigger('change'); 
     this_sel.find('#secs').val(seconds).trigger('change'); 

    } // End of count_exec(); 

    count_exec(); 

    interval = setInterval(count_exec, 1000); 

} 
// End of the main function 
}) (jQuery); 

,有一個HTML標記的位置:

<input class="knob" id="days"> 
<input class="knob" id="hours"> 
<input class="knob" id="mins"> 
<input class="knob" id="secs"> 

我希望每個元件輸出後添加文字。例如, 例如,在輸入日期之後,我想要放置「剩餘天數」。我怎樣才能做到這一點 ? 我想我應該編輯這些行:

  seconds = eventDate - currentDate;     // Find the number of seconds remaining 

     days =   Math.floor(seconds/(60 * 60 * 24));  // Divide to find the number of days remaining 
     seconds -=  days * 60 * 60 * 24;      // Subtract the number of (complete, => 24 hours) days calculated above 

     hours =   Math.floor(seconds/(60 * 60));   // Get the number of hours from that modified number^
     seconds -=  hours * 60 * 60; 

     minutes =  Math.floor(seconds/60); 
     seconds -=  minutes * 60; 

請參閱這些圖片明白我在問什麼...
什麼碼現在顯示:

http://uploadtak.com/images/j91_Untitled1.jpg

http://uploadtak.com/images/j91_Untitled1.jpg

我想要什麼要顯示:

http://uploadtak.com/images/v8243_Untitled2.jpg

回答

0

我不知道如果我undertood你的問題在所有...

我會做這樣的事情。

讓您的所有功能的剩餘時間(你已經有了),你得到的回報變種,例如時間

所以,你可以創建其他變種這樣

變種後文字= 「天」

和打印這樣的$("myresult).val(time+"<br />"+text);

+0

嘗試這個。但輸出是「NaN」 –

+0

其NaN可能會導致其中一個變量未定義 – jpganz18

+0

將'
'放入'input',真的嗎? –

2

使用jQuery after()insertAfter()功能。

this_sel.find('#days').val(days).after('<span>Remaining Days</span>').trigger('change'); 

$('<span>Remaining Days</span>').insertAfter(this_sel.find('#days')); 

UPDATE:

由於源代碼被調用在間隔,after()insertAfter()不是此情況下可用。

更好的解決方案是更改HTML並使用next()html()text()代替。新的HTML:

<input class="knob" id="days"><span></span> 
<input class="knob" id="hours"><span></span> 
<input class="knob" id="mins"><span></span> 
<input class="knob" id="secs"><span></span> 

新的jQuery設置文本後:

this_sel.find('#days').val(days).trigger('change').next().text('Remaining Days'); 
+0

我試過了你的代碼。但有一個問題,「剩餘天數」每隔一秒又一次增加到屏幕上... –

+0

ough,我看到...更改''爲''(每次輸入後添加空跨度)。並使用[next()](http://api.jquery.com/next/)和[text()](http://api.jquery.com/text/)或[html()](http: //api.jquery.com/html/)而不是'after()'。然後你得到'this_sel.find('#days').val(天).trigger('change')。next().text('Remaining Days');'。我也移動了'trigger()'調用,因爲'next()'改變了你在鏈中使用的jQuery對象。 –