根据所选选项转到页面

时间:2016-03-03 13:03:16

标签: javascript jquery

我正在为我的网站建立档案,但我被困住了。我希望用户选择指向该文件夹中页面的年,月和周。我无法解决捕获值所需的javascript并创建指导用户的条件。以及添加提交按钮。谢谢。

<select id="year" name="year">
  <option value="#">year?</option>
  <option value="2015">2015</option>
</select>

<select id="month" name="month">
  <option value="#">month?</option>
  <option value="jan">jan</option>
  <option value="feb">feb</option>
  <option value="mar">mar</option>
</select>

<select id="week" name="week">
  <option value="#">week?</option>
  <option value="wk1">1</option>
  <option value="wk2">2</option>
  <option value="wk3">3</option>
  <option value="wk4">4</option>
</select>
<input type="button" onclick="" value="Submit">

var year = $('#year').val();
var month = $('#month').val();
var week = $('#week').val();

//some more code...where I get stuck

if (year==2015 && month==jan && week==2) {
  window.location = "2015/jan/wk1/monday.html";
} else if (year==2015 && month==jan && week==2) {
  window.location = "2015 / jan / wk2 / monday.html";
}

4 个答案:

答案 0 :(得分:2)

编写代码而不是成功警报消息。这将解决你的希望。

&#13;
&#13;
var year = $('#year').val();
var month = $('#month').val();
var week = $('#week').val();
//some more code...where I get stuck
function urFunctin(){
if(document.getElementById("week").selectedIndex!=0)
  {
    if(document.getElementById("year").selectedIndex==0)
      {
        alert("Select year");
        document.getElementById("week").selectedIndex=0;
      }
    else if(document.getElementById("month").selectedIndex==0)
      {
        alert("Select month");
        document.getElementById("week").selectedIndex=0;
      }
    else{
    
      var year = $('#year').val();
      var month = $('#month').val();
      var week = $('#week').val();

      if(year && month && week)
      alert(year + "/" + month + "/" + week + "/monday.html");
    }
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="year" name="year">
  <option value="#">year?</option>
  <option value="2015">2015</option>
</select>

<select id="month" name="month">
  <option value="#">month?</option>
  <option value="jan">jan</option>
  <option value="feb">feb</option>
  <option value="mar">mar</option>
</select>

<select id="week" name="week">
  <option value="#">week?</option>
  <option value="wk1">1</option>
  <option value="wk2">2</option>
  <option value="wk3">3</option>
  <option value="wk4">4</option>
</select>
<br/><br/>
<input id="submit" type="button" onclick="urFunctin();" value="SUBMIT">
&#13;
&#13;
&#13;

答案 1 :(得分:1)

如果所有选择都具有某些值,我认为您希望将用户重定向到特定页面。 因此,为所有这些元素设置一个公共类,并将其设置为默认值为空字符串:

<select id="year" name="year" class="toRedirect">
  <option value="">year?</option>
  <option value="2015">2015</option>
</select>

<select id="month" name="month" class="toRedirect">
  <option value="">month?</option>
  <option value="jan">jan</option>
  <option value="feb">feb</option>
  <option value="mar">mar</option>
</select>

<select id="week" name="week" class="toRedirect">
  <option value="">week?</option>
  <option value="wk1">1</option>
  <option value="wk2">2</option>
  <option value="wk3">3</option>
  <option value="wk4">4</option>
</select>

然后处理更改事件:

$(function() { // document ready wrapper, could be stripped out
  $('.toRedirect').on('change', function() {
    var year = $('#year').val();
    var month = $('#month').val();
    var week = $('#week').val();

    if(year && month && week)
       window.location = year + "/" + month + "/" + week + "/monday.html";
       // what about day, only monday???
  });
});

答案 2 :(得分:0)

你对比较和逻辑语句有一些错误的想法,比较字符串时你需要在引号中添加字符串。

{
"data_to_send":[{
    "registration_ids":"id1, id2, id3",
    "data":{
        "data":"somedata for this users"
    }
},
{
    "registration_ids":"id4, id5, id6",
    "data":{
        "data":"somedata for users with ids 4, 5, 6"
    }
}]

答案 3 :(得分:0)

这将是用最少的JavaScript处理它的方法。

if ($('#year').val() != '#' 
  && $('#month').val() != '#' 
  && $('#week').val() != '#') {
    window.location = $('#year').val() + '/' 
      + $('#month').val() + '/' 
      + $('#week').val() + '/monday.html';
}

尽管如此,变量使它更具可读性。