方法一:用{htmloptions selected=$data.sex}
方法二:用<option{if $data.sex=='男'} selected{/if}>男</option>

解决方案 »

  1.   

    方法2是不是在只有定下来ITEM的时候才能使用?方法一应该是从数据库获取多item的时候也可以用的吧?
      

  2.   

    $select = array("a","b");
    $tpl->assign("select",$select);<select>
    <{foreach from=$select item=item}>
    <option value=<{$item}><{if $item=='b'}> selected<{/if}>><{$item}></option>
    <{/foreach}>
    </select>
      

  3.   

    除smarty以外,一般都需要显式的做selected的判断。就如楼上诸贴描述的那样smarty提供了另外的解决方法。这是手册中的示例
    index.php:require('Smarty.class.php');
    $smarty = new Smarty;
    $smarty->assign('cust_ids', array(1000,1001,1002,1003));
    $smarty->assign('cust_names', array('Joe Schmoe','Jack Smith','Jane
    Johnson','Carlie Brown'));
    $smarty->assign('customer_id', 1001);
    $smarty->display('index.tpl');index.tpl:<select name=customer_id>
        {html_options values=$cust_ids selected=$customer_id output=$cust_names}
    </select>
    index.php:require('Smarty.class.php');
    $smarty = new Smarty;
    $smarty->assign('cust_options', array(
                1001 => 'Joe Schmoe',
                1002 => 'Jack Smith',
                1003 => 'Jane Johnson',
                1004 => 'Charlie Brown'));
    $smarty->assign('customer_id', 1001);
    $smarty->display('index.tpl');index.tpl:<select name=customer_id>
        {html_options options=$cust_options selected=$customer_id}
    </select>
    OUTPUT: (both examples)<select name=customer_id>
        <option value="1000">Joe Schmoe</option>
        <option value="1001" selected="selected">Jack Smith</option>
        <option value="1002">Jane Johnson</option>
        <option value="1003">Charlie Brown</option>
    </select>
      

  4.   

    以上应该都是smarty的,那smartTemplate中呢,因为smartTemplate的资料少,它采用的是BEGIN/EDN的循环输入方式,在其中加入IF总是出错,有谁知道说明一下!
      

  5.   

    smarttemplate要麻烦一些------------ test.php ------------
    $tpl = new SmartTemplate( "test.htm" );$data = array( array( "value" => 1, "text" => "男"),
       array( "value" => 2, "text" => "女", "selected" => 1)
     );
    $tpl->assign("options", $data );
    $tpl->output();------------- test.htm  --------------
    <select name="t" id="t">
    <!-- BEGIN options -->
    <option value="{value}" <!-- IF selected -->selected<!-- ENDIF -->>{text}</option>
    <!-- END options -->
    </select>---------------------
    这种东西可以在客户端用JS来实现
    很简单 window.onload = function(){ document.getElementById("t").value={selected} }