在几秒钟内转换HH MM SS

时间:2015-12-03 09:29:08

标签: javascript

我需要使用javascript在几秒钟内转换3个表单输入(HH,MM,SS)。

我有这段代码,但它只能在几秒钟内输入1个表单:https://jsfiddle.net/94150148/hhomeLc3/

要做到这一点,我需要一个新的javascript函数。

window.onload = function () {generate()};

function generate() {

var width = 'width=\"' + document.getElementById('width').value + '\" ';
var height = 'height=\"' + document.getElementById('height').value + '\" ';
var ytid = "videoID";
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;

if (start !== "") {
    if(ytid === document.getElementById('ytid')) {
        ytid += '?start=' + start;
    }
    else {
        ytid += '&start=' + start;
    }
}
if (end !== "") {
    if (ytid === document.getElementById('ytid')) {
        ytid += '?end=' + end;
    }
    else {
        ytid += '&end=' + end;
    }
}
document.getElementById('embedcode').value = '<iframe ' + width + height +
'src=\"https://www.youtube.com\/embed\/' + ytid +
'\" frameborder=\"0\"><\/iframe>';
}

function clearall() {   
document.getElementById('width').value = 550;
document.getElementById('height').value = 315;
document.getElementById('start').value = "";
document.getElementById('end').value = "";
}

jsFiddle玩我需要的东西:https://jsfiddle.net/94150148/ybmkcyyu/

2 个答案:

答案 0 :(得分:1)

JS通常非常擅长数学。

    <message-source>
        <nucleus-name>
            /com/MessageChannel
        </nucleus-name>
        <output-port>
            <port-name>Port</port-name>
            <output-destination>
                <destination-name>
                    patchbay:/sqldms/com/SystemBroker
                </destination-name>
                <destination-type>Topic</destination-type>
            </output-destination>
        </output-port>
    </message-source>

<message-sink>
        <nucleus-name>
            /com/Broker
        </nucleus-name>
        <input-port>
            <port-name>Port</port-name>
            <input-destination>
                <destination-name>
                patchbay:/sqldms/com/SystemBroker
                </destination-name>
                <destination-type>
                    Topic
                </destination-type>
            </input-destination>
        </input-port>
    </message-sink>

https://jsfiddle.net/link2twenty/ybmkcyyu/4/

答案 1 :(得分:1)

https://jsfiddle.net/ybmkcyyu/3/

编辑: 当值为0时,不显示开始和结束 https://jsfiddle.net/ybmkcyyu/6/

EDIT2: https://jsfiddle.net/ybmkcyyu/7/

if (start !== "") {
    ytid += '?start=' + start;
}
if (end !== "") {
    if (start == "") {
        ytid += '?end=' + end;
    }
    else {
  ytid += '&end=' + end;
    }
}

你只需要得到每个字段的值,如int,然后用公式添加它:((小时* 60)+分钟)* 60 + secondes 你可以确保结果是一个数字。 (如果用户输入字符而不是数字,则不应显示错误)

var starth = parseInt(document.getElementById('starth').value);
var startm = parseInt(document.getElementById('startm').value);
var starts = parseInt(document.getElementById('starts').value);
var endh = parseInt(document.getElementById('endh').value);
var endm = parseInt(document.getElementById('endm').value);
var ends = parseInt(document.getElementById('ends').value);

var start = (((starth * 60) + startm) * 60) + starts;
if(isNaN(start) || start === 0)
   start = "";
var end = (((endh * 60) + endm) * 60) + ends;
if(isNaN(end) || end === 0)
  end = "";

/* (...) */