带有两个选择框的动态页面,iframe和jQuery(不含PHP)

时间:2016-01-07 00:44:26

标签: javascript jquery html iframe dynamic-pages

我正在尝试使用JavaScript创建动态页面。我使用jQuery。这个想法是避免使用PHP或其他服务器端语言。我使用selectboxes和iframe,但无法理解,为什么它不起作用。 “main.html”显示在片段中。这里没有错误,但网页上没有任何变化。目标路径是www / materials / dev1 / zad.html(选项1选择为“1”,选项2选择“zad”。依此类推。它使页面动态化。)文件main.html位于在www / main.html中。我在哪里犯了错误?
我尝试从不同的类中获取<select>,甚至从tagName获取但我无法做得更好。我认为它可能是另一个函数,而不是click()。你能告诉我一些有用的东西吗?

$(document).ready(function() {
  var aside_val = $('#aside_menu').val();
  //get the value of the first selection
  var header_val = $('#header_menu').val();
  //get the value of the second selection
  var folder = 'dev' + aside_val;
  //get the value of the folder
  $('.selection').click(function() {
    $('iframe#content').attr('src', "materials/" + folder + "/" + header_val + ".html");
    //change the src attribute of the iframe in order to get the proper page
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<select name="aside_menu" id="aside_menu" tabindex="1" style="top: 20px;" class="selection">
  <optgroup label="choose opt">
    <option class="opt" value="1">option 1</option>
    <option class="opt" value="2">option 2</option>
    <option class="opt" value="3">option 3</option>
  </optgroup>
</select>
<select name="header_menu" id="header_menu" tabindex="1" class="selection">
  <optgroup label="choose opt2">
    <option class="opt" value="zad">zad</option>
    <option class="opt" value="ooa">ООА</option>
    <option class="opt" value="scenario">scenario</option>
    <option class="opt" value="hipo">HIPO</option>
    <option class="opt" value="functions">functions</option>
  </optgroup>
</select>
<iframe src="materials/dev1/zad.html" width="986" height="600" align="center" id="content">no iframe</iframe>

1 个答案:

答案 0 :(得分:1)

问题是您只是在页面加载期间初始化click函数范围之外的变量。这只会将变量的值设置一次。您需要在click处理程序中声明它们。另外,由于iframe已经有id,因此您不需要$('iframe#content')。只需$('#content')即可。

 $(document).ready(function() {      
  //get the value of the folder
  $('.selection').click(function() {
    var aside_val = $('#aside_menu').val();
  //get the value of the first selection
    var header_val = $('#header_menu').val();
  //get the value of the second selection
    var folder = 'dev' + aside_val;
    $('#content').attr('src', "materials/" + folder + "/" + header_val + ".html");        
  });
});

示例:https://jsfiddle.net/DinoMyte/ejxt6y1c/1/