在模板中,例如smarty和smartTemplate,在编辑数据的时候,怎么对select的selected做设定? 方法一:用{htmloptions selected=$data.sex}方法二:用<option{if $data.sex=='男'} selected{/if}>男</option> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 方法2是不是在只有定下来ITEM的时候才能使用?方法一应该是从数据库获取多item的时候也可以用的吧? $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> 除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','JaneJohnson','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> 以上应该都是smarty的,那smartTemplate中呢,因为smartTemplate的资料少,它采用的是BEGIN/EDN的循环输入方式,在其中加入IF总是出错,有谁知道说明一下! 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} } php面试题,跪求准确答案 求一个简单的php查询语句 PHP中GD库的官方站文档中有显示ImageGifAnimBegin这个函数,为什么我用在PHP中会提示undefined function? 新手问个简单的问题 下拉列表 LINUX下大小写转换的问题 apache二级域名的设置问题,望高人来看看,帮帮小弟 写入文件的时候不要覆盖掉以前的内容,怎么写 想从事php的工作,现在(2017年9月)要学习哪个版本的php? 大佬求教php连接mysql数据库无法插入数据 如何一同输出变量名和它的值? QuickForm的一个小问题请教
$tpl->assign("select",$select);<select>
<{foreach from=$select item=item}>
<option value=<{$item}><{if $item=='b'}> selected<{/if}>><{$item}></option>
<{/foreach}>
</select>
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>
$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} }