选择(表格)中更改选定的选项 [英] Change selected option in select (form)

查看:150
本文介绍了选择(表格)中更改选定的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < select name =item [type]> 
< option value =0class =dr>第一个< / option>
< option value =1class =dr> Second< / option>
< option value =2class =dr>第三< / option>
< option value =3class =dr>第四< / option>
< / select>

我想检查变量 isset($ test)被定义。如果是这样,那么我想更改等于 $ test 的选项。像这样<选择选项>



例如。 $ test = 3 ;所以,选择的选项应该是第四个。如果 $ test 为空或未定义,那么第一个应该是选中的选项。

解决方案

< select name =item [type]id =selectBoxId>
< option value =0class =dr>第一个< / option>
< option value =1class =dr> Second< / option>
< option value =2class =dr>第三< / option>
< option value =3class =dr>第四< / option>
< / select>


< script type =text / javascript>
var test =<?= $ test;?>;
if(test!=''&& parseInt(test)){
document.getElementById('selectBoxId')。selectedIndex = test;
}
< / script>


I have an intial group of options like these:

<select name="item[type]">
    <option value="0" class="dr">First</option>
    <option value="1" class="dr">Second</option>
    <option value="2" class="dr">Third</option>
    <option value="3" class="dr">Fourth</option>
</select>

I want to check if a variable isset($test) is defined. If it is, then I want to change the option selected where the value is equal of $test. Something like this <OPTION SELECTED>

For example. $test = 3; so, the option selected should be fourth. If $test is empty or not defined, then the first should be the option that is selected.

解决方案

<select name="item[type]" id="selectBoxId">
<option value="0" class="dr">First</option>
<option value="1" class="dr">Second</option>
<option value="2" class="dr">Third</option>
<option value="3" class="dr">Fourth</option>
</select>


<script type="text/javascript">
    var test = "<?= $test; ?>";
    if (test != '' && parseInt(test)) {
        document.getElementById('selectBoxId').selectedIndex = test;
    }
</script>

这篇关于选择(表格)中更改选定的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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