0

我的 Switch 语句只执行第一种情况。我在适当的部分包含了休息时间,但它似乎不起作用。

我正在尝试获取当前日期并将该日期与表单日期进行比较,如果它在当前日期之前删除该表单选项。我已经能够使用简单的 if else 语句成功地完成此操作,但我认为 switch 语句会更快。

感谢所有帮助。

http://jsfiddle.net/Bpf8J/2/

JS

var checkapril1 = new Date();
checkapril1.setFullYear(2013, 3, 3);

var checkapril2 = new Date();
checkapril2.setFullYear(2013, 3, 17);

var checkmay1 = new Date();
checkmay1.setFullYear(2013, 4, 1);

var checkmay2 = new Date();
checkmay2.setFullYear(2013, 4, 15);

var checkmay3 = new Date();
checkmay3.setFullYear(2013, 4, 29);

var checkjune1 = new Date();
checkjune1.setFullYear(2013, 5, 12);

var checkjune2 = new Date();
checkjune2.setFullYear(2013, 5, 26);

var todaydate = new Date();
var e = document.getElementById("EventDate");

switch (true) {
case checkapril1 < todaydate:
    for (var i = 0; i < e.length; i++)
    var things = e.options[1];
    things.style.display = "none";
    things.disabled = true;
    break;

case checkapril2 < todaydate:
    for (var i = 0; i < e.length; i++)
    var things = e.options[2];
    things.style.display = "none";
    things.disabled = true;
    break;


case checkmay1 < todaydate:
    for (var i = 0; i < e.length; i++)
    var things = e.options[3];
    things.style.display = "none";
    things.disabled = true;
    break;


case checkmay2 < todaydate:
    for (var i = 0; i < e.length; i++)
    var things = e.options[4];
    things.style.display = "none";
    things.disabled = true;
    break;

case checkmay3 < todaydate:
    for (var i = 0; i < e.length; i++)
    var things = e.options[5];
    things.style.display = "none";
    things.disabled = true;
    break;

case checkjune1 < todaydate:
    for (var i = 0; i < e.length; i++)
    var things = e.options[6];
    things.style.display = "none";
    things.disabled = true;
    break;


case checkjune2 < todaydate:
    for (var i = 0; i < e.length; i++)
    var things = e.options[7];
    things.style.display = "none";
    things.disabled = true;
    break;

}

HTML

<form class="lpeRegForm formNotEmpty" method="post" enctype="application/x-www-form-      urlencoded" action="http://info.kinnser.com/index.php/leadCapture/save" id="mktForm_145"     name="mktForm_145">
<ul class='mktLblLeft'>
    <li class='mktFormReq mktField'>
            <label>EVENT DATE:</label><span class='mktInput'><select class='mktFormSelect mktFReq' name="EventDate" id="EventDate" size='1'   tabIndex='1'><option value='' selected='selected'>Please Select...</option><option value='April 3 2013'>April 3 2013</option><option value='April 17'>April 17 2013</option><option value='May 1'>May 1 2013</option><option value='May 15'>May 15 2013</option><option value='May 29'>May 29 2013</option><option value='June 12'>June 12 2013</option><option value='June 26'>June 26 2013</option></select><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktFormReq mktField'>
        <label>First Name:</label><span class='mktInput'><input class='mktFormText mktFormString mktFReq' name="FirstName" id="FirstName" type='text' value=""  maxlength='255' tabIndex='2' /><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktFormReq mktField'>
        <label>Last Name:</label><span class='mktInput'><input class='mktFormText mktFormString mktFReq' name="LastName" id="LastName" type='text' value=""  maxlength='255' tabIndex='3' /><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktFormReq mktField'>
        <label>Agency Name:</label><span class='mktInput'><input class='mktFormText mktFormString mktFReq' name="Company" id="Company" type='text' value=""  maxlength='255' tabIndex='4' /><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktFormReq mktField'>
        <label>Email Address:</label><span class='mktInput'><input class='mktFormText mktFormEmail mktFReq' name="Email" id="Email" type='text' value=""  maxlength='255' tabIndex='5' /><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktFormReq mktField'>
        <label>Zip Code:</label><span class='mktInput'><input class='mktFormText mktFormString mktFReq' name="PostalCode" id="PostalCode" type='text' value=""  maxlength='255' tabIndex='6' /><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktFormReq mktField'>
        <label>Phone Number:</label><span class='mktInput'><input class='mktFormText mktFormPhone mktFReq' name="Phone" id="Phone" type='text' value=""  maxlength='255' tabIndex='7' /><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktFormReq mktField'>
        <label>Role:</label><span class='mktInput'><select class='mktFormSelect mktFReq' name="Role__c" id="Role__c" size='1'   tabIndex='8'><option value='' selected='selected'>Please Select...</option><option value='Administrator'>Administrator</option><option value='Billing'>Billing</option><option value='CEO'>CEO</option><option value='CFO'>CFO</option><option value='Clinician'>Clinician</option><option value='Consultant'>Consultant</option><option value='D.O.N.'>D.O.N.</option><option value='Director of Operations (DOO)'>Director of Operations (DOO)</option><option value='I.T.'>I.T.</option><option value='Other'>Other</option><option value='Owner'>Owner</option><option value='Q.A.'>Q.A.</option><option value='Therapist'>Therapist</option></select><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktFormReq mktField'>
        <label>Medicare Census:</label><span class='mktInput'><select class='mktFormSelect mktFReq' name="Agency_Census_Range__c_account" id="Agency_Census_Range__c_account" size='1'   tabIndex='9'><option value='' selected='selected'>Please Select...</option><option value='0 - 50'>0 - 50</option><option value='51 - 125'>51 - 125</option><option value='126 - 250'>126 - 250</option><option value='251 - 500'>251 - 500</option><option value='501-750'>501-750</option><option value='751-1000'>751-1000</option><option value='1001+'>1001+</option></select><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktField' style="display: none;">
        <label>Lead Source:</label><span class='mktInput'><input class='mktFormHidden' name="LeadSource" id="LeadSource" type='hidden' value="Product Tour" /><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktField' style="display: none;">
        <label>Referral Code:</label><span class='mktInput'><input class='mktFormHidden' name="Event_Ref_Source__c" id="Event_Ref_Source__c" type='hidden' value="Product Tour" /><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktField' style="display: none;">
        <label>OfferID:</label><span class='mktInput'><input class='mktFormHidden' name="OfferID" id="OfferID" type='hidden' value="" /><span class='mktFormMsg'></span></span>
    </li>
    <li class='mktField mktLblRight'><span class='mktInput mktLblRight'><input class='mktFormCheckbox' name="SalesMeetingRequest" id="SalesMeetingRequest" type='checkbox' value="1"   tabIndex='13' /><label>Yes, I want to speak to a Kinnser Software solutions expert.</label>&nbsp;<span class='mktFormMsg'></span></span>
    </li>
    <li id='mktFrmButtons'>
        <label>&nbsp;</label>
        <input id='mktFrmSubmit' type='submit' style="width: auto; overflow: visible; padding-left: .25em; padding-right: .25em;" value='Register Now' name='submitButton' onclick='formSubmit(document.getElementById("mktForm_145")); return false;' />&nbsp;
        <input style='display: none;' id='mktFrmReset' type='reset' value='Clear' name='resetButton' onclick='formReset(document.getElementById("mktForm_145")); return false;' />
    </li>
</ul> <span style="display:none;"><input type="text" name="_marketo_comments" value="" /></span>

<input type="hidden" name="lpId" value="1811" />
<input type="hidden" name="subId" value="26" />
<input type="hidden" name="munchkinId" value="010-FKU-440" />
<input type="hidden" name="kw" value="" />
<input type="hidden" name="cr" value="" />
<input type="hidden" name="searchstr" value="" />
<input type="hidden" name="lpurl" value="http://info.kinnser.com/2013ProductTour.html?cr={creative}&kw={keyword}" />
<input type="hidden" name="formid" value="145" />
<input type="hidden" name="returnURL" value="http://info.kinnser.com/2013ProductTourThanks.html" />
<input type="hidden" name="retURL" value="http://info.kinnser.com/2013ProductTourThanks.html" />
<input type="hidden" name="returnLPId" value="1812" />
<input type="hidden" name="_mkt_disp" value="return" />
<input type="hidden" name="_mkt_trk" value="id:010-FKU-440&token:_mch-kinnser.com-1363892390400-55419" />
</form>
4

2 回答 2

4

switchJavaScript语句不是这样工作的。case语句上的值是静态评估的。您需要一系列if ... else子句。

另外,你的逻辑在我看来是倒退的。您从最小的开始按升序检查日期。因此,7 月的日期将满足第一个测试。

于 2013-05-30T17:02:11.713 回答
0

我不确定你应该做什么 for 循环,但我认为这不是你想要的。如果有,请说:

for (var i = 0; i < e.length; i++)
    var things = e.options[1];
    things.style.display = "none";
    things.disabled = true;

然后只执行第一行;您需要使用 { } 将它们包装在一个块中。

for (var i = 0; i < e.length; i++)
{
    var things = e.options[1];
    things.style.display = "none";
    things.disabled = true;
}

正如 Pointy 提到的,开关不是这项工作的合适工具。Switch 语句基于变量的离散值进行分支,例如:

switch(food)
{
    case 'apple':
        eat();
        break;

    case 'poison':
        donteat();
        break;
}

您应该将这些日期放在一个数组或其他东西中,这样如果您需要添加新日期会更容易。不过,要回答你的问题,我想我明白你想做什么;尝试这样的事情:

JavaScript

var check = [
    new Date(2013, 3, 3),
    new Date(2013, 3, 17),
    new Date(2013, 4, 1),
    new Date(2013, 4, 15),
    new Date(2013, 4, 29),
    new Date(2013, 5, 12),
    new Date(2013, 5, 26)
];

var todaydate = new Date();
var e = document.getElementById("EventDate");

for (var i = 0; i < e.length; i++)
{
    if (check[i] < todaydate)
    {
        var things = e.options[i];
            things.style.display = "none";
            things.disabled = true;
    }
}

这段代码所做的是将日期放在一个数组中并循环遍历它们。每次,如果日期在当前日期之前,它将找到选项元素并将其禁用。

http://jsfiddle.net/Bpf8J/12/

于 2013-05-30T17:16:51.077 回答