如何设置select选项的默认值
how to set a default value for select option
<script type="text/javascript">
function submitForm() {
var thelang = document.getElementById('lang').options[document.getElementById('lang').selectedIndex].value;
window.location.href = window.location.pathname + '?lang=' + thelang;
}
</script>
<?php session_start() ?>
<?php
require("config.php");
if(!isset($_GET['lang'])) {
$lng = 'en';
}
else {
$lng = $_GET['lang'];
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<label for="lang">Select Language:</label>
<select id="lang" name="lang" onchange="submitForm()">
<option value="en"<?php if( $_GET['lang'] != 'ar'){ echo " selected='"selected'"";} ?> > English</option>
<option value="ar"<?php if( $_GET['lang'] == 'ar'){ echo " selected='"selected'"";} ?> >Arabic</option>
</select>
</form>
<textarea cols="70" rows="5" readonly style="resize:none"><?php echo $trans['topnews']; ?> </textarea>
</body>
</html>
我是用PHP实现翻译的新手。我有一个文本,它从数据库中加载内容。一旦选定的值为英语,文本区域将为英语;一旦选定的数值为阿拉伯语,文本区域内容将为阿拉伯语。一切都很好,但在选择任何语言之前第一次加载页面时,我出现了错误(C:''examplep''htdocs''project''shownews.php中的未定义索引:lang(当我选择一个值时,这个错误不再显示
尝试这个
<option value="en"<?php if( $lng != 'ar'){ echo " selected='"selected'"";} ?> > English</option>
<option value="ar"<?php if( $lng == 'ar'){ echo " selected='"selected'"";} ?> >Arabic</option>
相关文章:
- 根据select选项元素将表单重定向到不同的URL
- jQuery动态表单显示在select选项上
- 如何在触发事件后选择select选项
- jQuery:根据select选项中的每页项目进行分页
- 使用JavaScript删除多个select选项
- 使用select选项转换时间.使用javascript
- 使用angularjs解析JSON,并选择select选项
- 使用jQuery在select选项上设置HTML数据属性
- 如何防止onclick事件在select选项中触发
- 如何在angular js中的select选项中获取所选项目id
- ng-select 选项在与自定义指令一起使用时加倍
- jQuery未能将JSON数据附加为select选项
- 如何将html中select选项的输入与javascript一起使用
- 使用VueJs获取Select选项文本
- 从jquery中的select选项添加两个不同的数组值(text&value)
- select选项在angularjs中不显示
- jQuery v 1.1x更改select选项的值
- 将select选项添加到id中
- 基于select选项的javascript动态表单生成
- Select选项包含在验证模型MVC时不起作用