$smarty - 如何动态更改选择框选项值

发布于 2024-12-06 03:28:29 字数 1698 浏览 1 评论 0原文

我是 smarty 的新手,所以请原谅我的清白 :oops:

我正在遵循以前的程序员留下的代码,并且我在根据另一个选择框的选定值动态更改选择框的值时遇到了这个问题。

所以情况是这样的:

我下拉了一个名为“Section”的下拉菜单,另一个下拉菜单名为“Subsection”。

我需要想到的是,当我选择一个部分时,该小节的值也会改变,并且仅显示所选部分下的小节。

这是问题的 javascript 模拟:

<html> 
<head> 
<title>Box changing demo</title> 
    <script type="text/javascript"> 

        var items = new Array(); 

        items[0] = new Array("Dog", "Cat", "Pig"); 
        items[1] = new Array("Andromeda", "Boötes", "Cepheus"); 
        items[2] = new Array("Mercury", "Venus", "Earth"); 
        items[3] = new Array("BMW", "Audi", "Bugatti"); 

        function changeItems(){; 
            num=document.changer.section.options[document.changer.section.selectedIndex].value; 
            document.changer.subsection.options.length = 0; 
            for(i=0; i<items[num].length; i++){ 
                document.changer.subsection.options[i] = new Option(items[num][i], items[num][i]); 
            } 
        } 

    </script> 
</head> 
<body> 

    <form name="changer"> 
         <select name="section" onchange="changeItems();"> 
              <option value="0">Animals</option> 
              <option value="1">Constelations</option> 
              <option value="2">Planets</option> 
              <option value="3">Cars</option> 
         </select> 

         <select name="subsection"> 
          <!--<option>tgntgn</option> -->
         </select> 
    </form> 

</body> 
</html>

这就是我需要用 smarty 做的事情。

有人吗?

感谢您的帮助。

I am a newbie of smarty so please pardon my innocence :oops:

I am following a code left by previous programmer and i have this problem on dynamically changing the values of a select box depending on the selected value of another select box.

So here's the situation:

I have drop down named "Section" and another one named "Subsection".

What i need to come up with is that when i choose a Section the Values of the Subsection will change too and only displays the Subsections which is under that section selected.

here's a javascript simulation of the problem:

<html> 
<head> 
<title>Box changing demo</title> 
    <script type="text/javascript"> 

        var items = new Array(); 

        items[0] = new Array("Dog", "Cat", "Pig"); 
        items[1] = new Array("Andromeda", "Boötes", "Cepheus"); 
        items[2] = new Array("Mercury", "Venus", "Earth"); 
        items[3] = new Array("BMW", "Audi", "Bugatti"); 

        function changeItems(){; 
            num=document.changer.section.options[document.changer.section.selectedIndex].value; 
            document.changer.subsection.options.length = 0; 
            for(i=0; i<items[num].length; i++){ 
                document.changer.subsection.options[i] = new Option(items[num][i], items[num][i]); 
            } 
        } 

    </script> 
</head> 
<body> 

    <form name="changer"> 
         <select name="section" onchange="changeItems();"> 
              <option value="0">Animals</option> 
              <option value="1">Constelations</option> 
              <option value="2">Planets</option> 
              <option value="3">Cars</option> 
         </select> 

         <select name="subsection"> 
          <!--<option>tgntgn</option> -->
         </select> 
    </form> 

</body> 
</html>

This is what i need to do with smarty.

Anybody?

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彻夜缠绵 2024-12-13 03:28:29

你不能在 Smarty 中做到这一点,因为它不能仅用 HTML 来完成。为此,您必须使用 Javascript。看看 http://www.texotela.co.uk/code/jquery/select/ - 看起来很容易实现。

You can't do it in Smarty, because it just cannot be done with HTML only. You have to use Javascript for this. Look at http://www.texotela.co.uk/code/jquery/select/ - it seems easy enough to implement.

青朷 2024-12-13 03:28:29

希望这能回答您的问题

<!--JAVASCRIPT-->
<!--CREATE DROPBOX WHEN SEC1 IS SELECTED-->
<script>
var section= document.getElementById("section").value;
if(section == SEC1){
document.getElementById("subSEC").innerHTML='
  <select name="subsection" type="text" id="subsection">
  <option value=""></option>
  <option value="Sub1">Sub1</option>
  <option value="Sub2">Sub2</option>
  <option value="Sub3">Sub3</option>
  </select>"
';
}


</script>
<!--HTML MARKUP-->

<select name="section" type="text" id="section">
      <option value=""></option>
      <option value="SEC1">SEC1</option>
      <option value="SEC2">SEC2</option>
      <option value="SEC3">SEC3</option>
      </select>

<!--SPACE TO PLACE DROPBOX FROM JAVASCRIPT-->
<span id="subSEC">

</span>

Hope This Answers Your Question

<!--JAVASCRIPT-->
<!--CREATE DROPBOX WHEN SEC1 IS SELECTED-->
<script>
var section= document.getElementById("section").value;
if(section == SEC1){
document.getElementById("subSEC").innerHTML='
  <select name="subsection" type="text" id="subsection">
  <option value=""></option>
  <option value="Sub1">Sub1</option>
  <option value="Sub2">Sub2</option>
  <option value="Sub3">Sub3</option>
  </select>"
';
}


</script>
<!--HTML MARKUP-->

<select name="section" type="text" id="section">
      <option value=""></option>
      <option value="SEC1">SEC1</option>
      <option value="SEC2">SEC2</option>
      <option value="SEC3">SEC3</option>
      </select>

<!--SPACE TO PLACE DROPBOX FROM JAVASCRIPT-->
<span id="subSEC">

</span>
权谋诡计 2024-12-13 03:28:29

尝试类似的操作:

{html_options name=section options=$options selected=$index 
    onchange="changeItems();"}

其中 $options 和 $index (选择标签中选定选项的索引)在 php 中分配:

<?php
...
$smarty = new Smarty ();
...
$smarty->assign ( 'index', $some_php_val );
$smarty->assign ( 'options', $some_php_array );
...
?>

有关详细信息,请检查:

http://www.smarty.net/docsv2/en/language.function.html.options.tpl
http://www.smarty.net/forums/viewtopic.php?p=53422

try something like:

{html_options name=section options=$options selected=$index 
    onchange="changeItems();"}

where $options and $index (index of selected option in select tag) are assigned in php:

<?php
...
$smarty = new Smarty ();
...
$smarty->assign ( 'index', $some_php_val );
$smarty->assign ( 'options', $some_php_array );
...
?>

for more info check:

http://www.smarty.net/docsv2/en/language.function.html.options.tpl
http://www.smarty.net/forums/viewtopic.php?p=53422

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文