0

我有这个 html 标记:

<div class="table">
            <div class="col1 right">Start:

            </div>
            <div class="col2outer center">
                <div class="col2mid">
                    <asp:DropDownList ID="ddlStartHour" runat="server" onchange="Calculate();">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>

</asp:DropDownList>
                </div>
                <div class="col2side">
                    <asp:DropDownList ID="ddlStartMinute" runat="server" onchange="calculateTime()">
 <asp:ListItem Value="0">0</asp:ListItem>
 <asp:ListItem Value="30">30</asp:ListItem>
 </asp:DropDownList>
                </div>
            </div>
        </div>
        <div class="table">
            <div class="col1 right">Finish:

            </div>
            <div class="col2outer center">
                <div class="col2mid">
                    <asp:DropDownList ID="ddlFinishHour" runat="server" onchange="Calculate();">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem></asp:DropDownList>
                </div>
                <div class="col2side">
                    <asp:DropDownList ID="ddlFinishMinute" runat="server" >
<asp:ListItem Value="0">0</asp:ListItem>
<asp:ListItem Value="30">30</asp:ListItem>

 </asp:DropDownList>
                </div>
            </div>
        </div>
 <div class="margin1">
            <h4>total</h4>
             <h3><asp:Label ID="Label1" runat="server">0</asp:Literal></h3>
             <h4>hrs</h4>
    </div>

当用户选择发生变化时,我想在客户端进行计算。我已经看到很多帖子试图使用普通的 html 元素来做到这一点。但就我而言,我需要使用 asp.net 控件。我尝试使用此 javascript ( http://jsfiddle.net/VnwF7/4/ ) 代码,但它不适用于 asp.net 控件。

function Calculate() {
    //get values
    var valuestart = $("#<%= ddlStartH.ClientID %>").val();
var valuestop = $("#<%= ddlFinishH.ClientID %>").val();

//create date format          
var timeStart = new Date("01/01/2007 " + valuestart).getHours();
var timeEnd = new Date("01/01/2007 " + valuestop).getHours();

var hourDiff = timeEnd - timeStart - 0.5;

$('#<%= lblDuration.ClientID %>').val(hourDiff);

}

谢谢你的帮助。

4

1 回答 1

1

尝试以下操作:

var timeStart = new Date("01/01/2007 " + valuestart + ":00:00").getHours();
var timeEnd = new Date("01/01/2007 " + valuestop + ":00:00").getHours();

代替:

var timeStart = new Date("01/01/2007 " + valuestart).getHours();
var timeEnd = new Date("01/01/2007 " + valuestop).getHours();
于 2013-10-24T04:45:03.923 回答