在以下情况下,如何保持在选择HTML控件中选择的特定值? [英] How to keep a specific value selected in a select HTML control in following scenario?

查看:96
本文介绍了在以下情况下,如何保持在选择HTML控件中选择的特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网站使用PHP,Smarty和MySQL.我有一个选择控件.它的代码如下:

I'm using PHP, Smarty and MySQL for my website. I'm having a select control. The code for it is as follows:

<select name="contact_label" id="set_contact_label">
  <option value=""> -- Select label-- </option>
  {if $enquiries_labels}
    {foreach from=$enquiries_labels item=label key=key}
  <option value="{$key}" {if $contact_label == $key} selected="selected" {/if}>{$label}</option>
    {/foreach}
  {/if}
</select>

等效的HTML输出如下:

Equivalent HTML output is as follows:

<select name="contact_label" id="contact_label">
  <option value=""> -- Select label-- </option>
  <option value="0" selected="selected">New Enquiry</option>
  <option value="1" >Retail Enquiry</option>
  <option value="2" >Feedback</option>
  <option value="3" >Payment Query</option>
  <option value="4" >Package Query</option>
  <option value="5" >Test Query</option>
</select>

现在,我的问题是,如果条件失败,则默认要选择选项-选择标签-.如果满足if条件,则应选择该选项值.现在,如果if条件失败,则在选择框中将值新查询" 保持选中状态.实际上,我希望在if条件失败时默认选择--Select label-值.我为此付出了很多努力,但未能成功.有人可以在这方面帮助我吗?

Now my issue is I want the option --Select label-- selected by default when the if condition gets failed. If the if condition is satisfied then that option value should get selected. Now what's happening is the value "New Enquiry" is kept selected in the select box when the if condition fails. Actually I want the value --Select label-- to be selected by default when the if condition fails. I tried a lot to achieve this but couldn't succeed. Can anyone please help me in this regard?

推荐答案

您应该尝试一下 在php文件中,您需要:

You should try this In php file you need :

$ smarty-> assign('MyArray',$ enquiries_labels); $ smarty-> assign('selectedOption',0);

$smarty->assign('MyArray',$enquiries_labels); $smarty->assign('selectedOption',0);

现在在模板中

<select name="contact_label" id="set_contact_label"> 
            <option value="0">Please Select label</option> 
            {html_options options=$MyArray selected=$selectedOption}
        </select>

这篇关于在以下情况下,如何保持在选择HTML控件中选择的特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆