如何在jquery val(val)中回显文本?

时间:2013-06-26 21:55:10

标签: javascript jquery

有一个jquery代码:

(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

2 个答案:

答案 0 :(得分: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'));

<强>更新

由于源代码是在时间间隔内调用的,因此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');

答案 1 :(得分:0)

我不知道我是否提出了你的问题......

我会做这样的事情。

获得所有功能后剩下的时间(你已经拥有)你会在返回变量中得到它,例如时间

所以,你可以像这样创建其他var

var text =“days”

并打印类似$("myresult).val(time+"<br />"+text);

的内容
相关问题