繁体   English   中英

Javascript 3下拉菜单。 更改一个下拉菜单时,请为其他两个下拉菜单设置默认值。 需要优化

[英]Javascript 3 drop down menus. On change of one drop down menu set default value for other two drop down menus. Need optimization

代码很长。 但是从我的观点来看,必须显示整个代码。

三个下拉菜单。 PHP和JavaScript的使用。 如果在菜单更改值之一中,则将其他两个菜单中的值设置为默认值。

创建的长代码需要改进/优化(需要使用JavaScript来缩短代码长度)。

在此处发布代码(无php) http://jsfiddle.net/rigaconnect/QaUmK/5/ (出于无法理解的原因,在jsfiddle中发布的代码无法正常工作;在我的网站上,该代码按预期工作。)

起初php

$options_month = array("Select month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$options_quarter = array("Select quarter", "First", "Second", "Third", "Fourth");
$options_halfyear = array("Select half-year", "First", "Second");

然后下拉菜单

<select id="month_id" name="monthid">
<?php
foreach ( $options_month as $i1=>$opt1 ) : 
echo '<option value="' .htmlspecialchars($i1) .'"' .((htmlspecialchars($i1) == htmlspecialchars($_POST['monthid'][$counter]))? 'selected' : "") .'>'.htmlspecialchars($opt1) .'</option>';
endforeach;
?>
</select>

<select id="quarter_id" name="quarterid">
<?php
foreach ( $options_quarter as $i1=>$opt1 ) : 
echo '<option value="' .htmlspecialchars($i1) .'"' .((htmlspecialchars($i1) == htmlspecialchars($_POST['quarterid'][$counter]))? 'selected' : "") .'>'.htmlspecialchars($opt1) .'</option>';
endforeach;
?>
</select>

<select id="halfyear_id" name="halfyearid">
<?php
foreach ( $options_halfyear as $i1=>$opt1 ) : 
echo '<option value="' .htmlspecialchars($i1) .'"' .((htmlspecialchars($i1) == htmlspecialchars($_POST['halfyear'][$counter]))? 'selected' : "") .'>'.htmlspecialchars($opt1) .'</option>';
endforeach;
?>
</select>

然后需要改进的javascript。 代码很长。 可能对于较短的代码也可以这样做。

var month_for_update = document.getElementById( 'month_id' );
function updateQuarterHalfyear ( e ) {

var quarter = this.options[ this.selectedIndex ].value, quarterDDL= document.getElementById( 'quarter_id' ), quarter, i = quarterDDL.options.length - 1;

var halfyear = this.options[ this.selectedIndex ].value, halfyearDDL= document.getElementById( 'halfyear_id' ), halfyear, i = halfyearDDL.options.length - 1;

quarter = 0;
halfyear = 0;

for ( ; i > -1 ; i-- ) {
if ( quarterDDL.options[i].value == quarter ) {
quarterDDL.options[i].selected = true;
break;
}
}

for ( ; i > -1 ; i-- ) {
if ( halfyearDDL.options[i].value == halfyear ) {
halfyearDDL.options[i].selected = true;
break;
}
}

}

month_for_update.onchange = updateQuarterHalfyear;

/**/
var quarter_for_update = document.getElementById( 'quarter_id' );
function updateMonthHalfyear ( e ) {

var month = this.options[ this.selectedIndex ].value, monthDDL= document.getElementById( 'month_id' ), month, i = monthDDL.options.length - 1;

var halfyear = this.options[ this.selectedIndex ].value, halfyearDDL= document.getElementById( 'halfyear_id' ), halfyear, i = halfyearDDL.options.length - 1;

month = 0;
halfyear = 0;

for ( ; i > -1 ; i-- ) {
if ( monthDDL.options[i].value == month ) {
monthDDL.options[i].selected = true;
break;
}
}

for ( ; i > -1 ; i-- ) {
if ( halfyearDDL.options[i].value == halfyear ) {
halfyearDDL.options[i].selected = true;
break;
}
}

}

quarter_for_update.onchange = updateMonthHalfyear;


/**/
var halfyear_for_update = document.getElementById( 'halfyear_id' );
function updateMonthQuarter ( e ) {

var month = this.options[ this.selectedIndex ].value, monthDDL= document.getElementById( 'month_id' ), month, i = monthDDL.options.length - 1;

var quarter = this.options[ this.selectedIndex ].value, quarterDDL= document.getElementById( 'quarter_id' ), quarter, i = quarterDDL.options.length - 1;

month = 0;
quarter = 0;

for ( ; i > -1 ; i-- ) {
if ( monthDDL.options[i].value == month ) {
monthDDL.options[i].selected = true;
break;
}
}

for ( ; i > -1 ; i-- ) {
if ( quarterDDL.options[i].value == quarter ) {
quarterDDL.options[i].selected = true;
break;
}
}

}

halfyear_for_update.onchange = updateMonthQuarter;    

请建议如何缩短javascript。 可能可以对代码的其他部分进行改进。

澄清。 默认值为$options_month$options_quarter$options_halfyear的第一个值( [0] )。 页面首次加载时可以看到“ Select month ,“ Select quarter ,“ Select half-year 因此页面加载。 用户从月份下拉菜单中选择例如February月。 然后,用户决定代替月份需要选择季度。 用户从四分之一下拉菜单中选择“ First 月份值下拉菜单会自动更改为Select month

这是我的解决方案。 简单得多。 但是,我也更改了html布局。

HTML更新:(在select标签中具有onchange)

<select id="month_id" name="monthid" onchange="update_other_dd ( 'month_id' )">
...
<select id="quarter_id" name="quarterid" onchange="update_other_dd ( 'quarter_id' )">
...
<select id="halfyear_id" name="halfyearid" onchange="update_other_dd ( 'halfyear_id' )">
...

Javascript更新:(减少到9行)

    var dd_array = ['month_id', 'quarter_id', 'halfyear_id'];
    function update_other_dd ( dd_id ) {
    for(i=0;i<dd_array.length;i++){
    if(dd_array[i] != dd_id){
        var array_to_reset = document.getElementById( dd_array[i] );
        array_to_reset.options[0].selected = 1;
    }
    }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM