如何为数组分配链接

时间:2015-11-11 00:14:04

标签: javascript html

我正在尝试为每个阵列分配链接,以便每个主题都可以使用。在我的代码中,我希望Q1,Q2,Q3和Q4具有不同网页的不同链接。因此,当按下提交按钮时,用户将被带到网页。 例如,用户将转到网页,点击主题,点击问题,当他们点击提交时,他们将被带到问题页面(.html或其他)。



var subjects = new Array("English", "Irish", "Maths", "German", "History", "Business", "Geogrpahy");
var questions = new Array();
questions["English"] = new Array("Q1", "Q2", "Q3", "Q4");
questions["Irish"] = new Array("Q1", "Q2", "Q3", "Q4");
questions["Maths"] = new Array("Q1", "Q2", "Q3", "Q4");
questions["German"] = new Array("Q1", "Q2", "Q3", "Q4");
questions["History"] = new Array("Q1", "Q2", "Q3", "Q4");
questions["Business"] = new Array("Q1", "Q2", "Q3", "Q4");
questions["Geogrpahy"] = new Array("Q1", "Q2", "Q3", "Q4");

function resetForm(theForm) {
  /* reset subjects */
  theForm.subjects.options[0] = new Option("Please select a subject", "");
  for (var i = 0; i < subjects.length; i++) {
    theForm.subjects.options[i + 1] = new Option(subjects[i], subjects[i]);
  }
  theForm.subjects.options[0].selected = true;
  /* reset questions */
  theForm.questions.options[0] = new Option("Please select a question", "");
  theForm.questions.options[0].selected = true;
}

function updateModels(theForm) {
  var make = theForm.subjects.options[theForm.subjects.options.selectedIndex].value;
  var newModels = questions[make];
  theForm.questions.options.length = 0;
  theForm.questions.options[0] = new Option("Please select a question", "");
  for (var i = 0; i < newModels.length; i++) {
    theForm.questions.options[i + 1] = new Option(newModels[i], newModels[i]);
  }
  theForm.questions.options[0].selected = true;
}

resetForm(document.autoSelectForm);
&#13;
<form name="autoSelectForm" action="" method="post">
  <select size="1" name="subjects" onchange="updateModels(this.form)">
  </select>
  <select size="1" name="questions" onclick="">
  </select>
  <input type="submit">
</form>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

根据您的变量和数据,为每个问题重写此代码new Array("Q1", "Q2", "Q3", "Q4");并没有多大意义,因为以这种方式执行它似乎是多余的。这可以通过这种方式简化:

P.S。 纯javascript 对于初学者来说太方便了,请使用 jQuery 而不是从here获取jquery

HTML

// we use target="_blank" and method="get" here just to show that it works
<form id="myform" name="autoSelectForm" action="http://example.com/something.php" method="get" target="_blank">
  <select size="1" name="subjects" id="subjects"></select>
  <select size="1" name="quarters" id="quarters"></select>
  <input type="submit">
</form>

JS

var subjects = [
     {
        subject: "English", quarters: [
          {name: "Q1", link: "http://example.com/engQ1.html"}, 
          {name: "Q2", link: "http://example.com/engQ2.html"}
        ]
     },
    {
      subject: "Irish", quarters: [
        {name: "Q1", link: "http://example.com/irQ1.html"}, 
        {name: "Q2", link: "http://example.com/irQ2.html"}
      ]
    }
]

var s_subj = $('#subjects');
var s_quar = $('#quarters');

setDropdownOptions(subjects);
refreshQuarters();  // load the quarters for the first subject

    function setDropdownOptions(values){
       var str_subjects = '';
        $.each(subjects, function(i, row){
          str_subjects+= '<option>'+row.subject+'</option>'; 
        });
       s_subj.html(str_subjects);
    }

   function refreshQuarters(){
       var current_subj = s_subj.val();
        $.each(subjects, function(i, row){
             if(current_subj == subjects[i].subject){
                 var str_quarters = "";
                 // loop through the subject's quarters
                 $.each(row.quarters, function(col, obj){
                    // we use data-link in storing each individual urls
                    str_quarters += '<option data-link="'+obj.link+'">'+obj.name+'</option>';
                 });
                 s_quar.html(str_quarters);
             }
        });
   }

  // add listener when subject is changed
  s_subj.change(function(){
    refreshQuarters();
  });

  // change the form action before submitting the form
  $("#myform").submit(function(){
      // get the link from the selected quarter through data-link
      var link = $("#quarters option:selected").data("link");
     $(this).attr("action", link);
  });

DEMO:https://jsfiddle.net/hs3oLrns/1/

答案 1 :(得分:0)

将“提交”输入更改为按钮,添加“提交”功能以与按钮的onclick属性配对。我创建的提交函数只是将选中的主题和问题添加到我创建的一些“p”HTML元素中。如果您希望按钮将用户带到单独的URL,也许您可​​以使用window.url =“”创建一个函数。哦,我删除了第二个脚本标签,并添加了一个事件监听器,等待html在执行前完全加载。

function submit(){
  var subjects = document.getElementById("subjects").value
  var questions = document.getElementById("questions").value
  document.getElementById("ithquestion").innerHTML = questions
  document.getElementById("ithsubject").innerHTML = "Subject: " +  subjects



}

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById("button").addEventListener('click',submit,false)
  resetForm(document.autoSelectForm);
})


</script>

<body>
  <form name="autoSelectForm" action="" method="post" >
    <select size="1" name="subjects" id = "subjects"        onchange="updateModels(this.form)">
    </select>
    <select size="1" name="questions" id = "questions">
    </select>
    <input type="button" value = "button" id = "button"/>
 </form>
 <p id = "ithsubject"></p>
 <p id = "ithquestion"></p>
</body>