将下拉框设置为在Javascript中选中
我正在我的应用程序中使用Javascript。在我的表中,有一个名为将下拉框设置为在Javascript中选中,javascript,Javascript,我正在我的应用程序中使用Javascript。在我的表中,有一个名为industry的列,其中包含如下值 id 69 name :aruna username :aruna email :artkjnjkn@sf.sd password: bd09935e199eab3d48b306d181b4e3d1:i0ODTgVXbWyP1iFOV type : 3 industry: | Insurance | Analytics | EIS and Pro
industry
的列,其中包含如下值
id 69
name :aruna
username :aruna
email :artkjnjkn@sf.sd
password: bd09935e199eab3d48b306d181b4e3d1:i0ODTgVXbWyP1iFOV
type : 3
industry: | Insurance | Analytics | EIS and Process Engineering
实际上,该行业值是从多选下拉框中插入的
现在我正在尝试像加载一样使我的表单包含这些值
工业在哪里?下拉框
<select id="ind1" moslabel="Industry" onClick="industryfn();"mosreq="0" multiple="multiple" size="3" class="inputbox" name="industry1[]">'+
<option value="Banking and Financial Services">Banking and Financial Services</option>
<option value="Insurance">Insurance</option>
<option value="Telecom">Telecom</option>
<option value="Government ">Government </option>
<option value="Healthcare & Life sciences">Healthcare & Life sciences</option>
<option value="Energy">Energy</option>
<option value="Retail &Consumer products">Retail &Consumer products</option>
<option value="Energy, resources & utilities">Energy, resources & utilities</option>
<option value="Travel and Hospitality">Travel and Hospitality</option>
<option value="Manufacturing">Manufacturing</option>
<option value="High Tech">High Tech</option>
<option value="Media and Information Services">Media and Information Services</option>
</select>
'+
银行和金融服务
保险
电信
政府
医疗保健与生命科学
能量
零售及消费品
能源、资源和公用事业
旅行和招待
制造业
高科技
媒体和信息服务
如何保持选定的行业价值(保险、分析、环境影响报告和过程工程)
编辑:
Window.onDomReady(函数(){
用户->获取(“行业”);
$s=分解(“|”,$str)
?>
var selectedFields = new Array();
<?php for($i=1;$i<count($s);$i++){?>
selectedFields.push("<?php echo $s[$i];?>");
<?php }?>
for(i=1;i<selectedFields.length;i++)
{
var select=selectedFields[i];
for (var ii = 0; ii < document.getElementById('ind1').length; ii++) {
var value=document.getElementById('ind1').options[ii].value;
alert(value);
alert(select);
if(value==select)
{
document.getElementById('ind1').options[ii].selected=selected;
}//If
} //inner For
}//outer For
</script>
var selectedFields=new Array();
selectedFields.push(“”);
对于(i=1;i将所选值分解为一个数组,进行foreach循环以遍历您的select的所有选项,如果这些值匹配,只需将selected=“selected”放在那里即可
示例:
Javascript/PHP:
Array.prototype.in_数组=函数(p_val){
for(var i=0,l=this.length;i
HTML:
银行和金融服务
保险
电信
政府
保健和生命科学
能量
零售及消费品
能源、资源和公用事业
旅行和招待
制造业
高科技
媒体和信息服务
我不建议混合使用PHP和Javascript代码tho。这太混乱了,很难维护。我建议您在服务器端(PHP)或客户端(AJAX)解析这些数据。请参阅我的编辑。.我已经按照您所说的做了。.但如果函数不起作用。y所以??用一个示例编辑了我的答案。
<script type="text/javascript">
Array.prototype.in_array = function( p_val ) {
for(var i = 0, l = this.length; i < l; i++) {
if(this[i] == p_val) {
return true;
}
}
return false;
}
// Swap this for normal onDomReady if you don't use jQuery
$(document).ready( function() {
<?php
$selected_from_db = "Insurance|Analytics|EIS and Process Engineering|Telecom";
$selected = explode( "|", $selected_from_db );
?>
var selected_fields = new Array();
<?php
foreach ( $selected as $key => $value ) {
?>
selected_fields.push("<?php echo $value;?>");
<?php
}
?>
var ind_dropdown = document.getElementById( "ind1" );
for ( var i = 0; i < ind_dropdown.length; i++ )
{
if ( selected_fields.in_array( ind_dropdown.options[ i ].text ) ) {
ind_dropdown.options[ i ].setAttribute( "selected", "selected" );
}
/*var select=selectedFields[i];
for (var ii = 0; ii < document.getElementById('ind1').length; ii++) {
var value=document.getElementById('ind1').options[ii].value;
alert(value);
alert(select);
if(value==select)
{
document.getElementById('ind1').options[ii].selected=selected;
}
}*/
}
});
</script>
<body>
<select id="ind1" name="industry[]" multiple="multiple" size="5">
<option value="Banking and Financial Services">Banking and Financial Services</option>
<option value="Insurance">Insurance</option>
<option value="Telecom">Telecom</option>
<option value="Government ">Government </option>
<option value="Healthcare & Life sciences">Healthcare & Life sciences</option>
<option value="Energy">Energy</option>
<option value="Retail &Consumer products">Retail & Consumer products</option>
<option value="Energy, resources & utilities">Energy, resources & utilities</option>
<option value="Travel and Hospitality">Travel and Hospitality</option>
<option value="Manufacturing">Manufacturing</option>
<option value="High Tech">High Tech</option>
<option value="Media and Information Services">Media and Information Services</option>
</select>
</body>