动态查找控件-Javascript [英] Dynamically finding controls - Javascript

查看:60
本文介绍了动态查找控件-Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java语言很陌生,无法解决这个问题.

我有一个方法,可以通过许多控件来更改属性,以更改其他控件的文本.
目前,我只是使用了一个过长的if-else语句来解决这个问题.
如果有人知道获得相同最终结果的更有效方法,请提供帮助.

我们将不胜感激.

提前非常感谢!

以下是我目前正在使用并想优化的方法.

I''m quite new to Javascript and cannot figure this out.

I have a method that gets called on property change by quite a number of controls to change text of other controls.
At the moment I just use an excessively long if-else statement to get this right.
If anyone knows a more effective way of getting the same end result, please help.

It would be greatly appreciated.

Many thanks in advance!

Below is the method I''m currently using and would like to optimize.

function textChangedEvent(current, next) {
var a = null;
if (next == 'TextBoxAddShiftRulesStartTimeTuesday') {
a = document.getElementById('<%= TextBoxAddShiftRulesStartTimeTuesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesStartTimeWednesday') {
a = document.getElementById('<%= TextBoxAddShiftRulesStartTimeWednesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesStartTimeThursday') {
a = document.getElementById('<%= TextBoxAddShiftRulesStartTimeThursday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesStartTimeFriday') {
a = document.getElementById('<%= TextBoxAddShiftRulesStartTimeFriday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesStartTimeSaturday') {
a = document.getElementById('<%= TextBoxAddShiftRulesStartTimeSaturday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesStartTimeSunday') {
a = document.getElementById('<%= TextBoxAddShiftRulesStartTimeSunday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesStartTimePPH') {
a = document.getElementById('<%= TextBoxAddShiftRulesStartTimePPH.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesFinishTimeTuesday') {
a = document.getElementById('<%= TextBoxAddShiftRulesFinishTimeTuesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesFinishTimeWednesday') {
a = document.getElementById('<%= TextBoxAddShiftRulesFinishTimeWednesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesFinishTimeThursday') {
a = document.getElementById('<%= TextBoxAddShiftRulesFinishTimeThursday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesFinishTimeFriday') {
a = document.getElementById('<%= TextBoxAddShiftRulesFinishTimeFriday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesFinishTimeSaturday') {
a = document.getElementById('<%= TextBoxAddShiftRulesFinishTimeSaturday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesFinishTimeSunday') {
a = document.getElementById('<%= TextBoxAddShiftRulesFinishTimeSunday.ClientID %>');
}
else if (next == 'TextBoxAddShiftRulesFinishTimePPH') {
a = document.getElementById('<%= TextBoxAddShiftRulesFinishTimePPH.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsHoursTuesday') {
a = document.getElementById('<%= TextBoxAddDeductionsHoursTuesday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsHoursWednesday') {
a = document.getElementById('<%= TextBoxAddDeductionsHoursWednesday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsHoursThursday') {
a = document.getElementById('<%= TextBoxAddDeductionsHoursThursday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsHoursFriday') {
a = document.getElementById('<%= TextBoxAddDeductionsHoursFriday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsHoursSaturday') {
a = document.getElementById('<%= TextBoxAddDeductionsHoursSaturday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsHoursSunday') {
a = document.getElementById('<%= TextBoxAddDeductionsHoursSunday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsHoursPPH') {
a = document.getElementById('<%= TextBoxAddDeductionsHoursPPH.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsDeductTuesday') {
a = document.getElementById('<%= TextBoxAddDeductionsDeductTuesday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsDeductWednesday') {
a = document.getElementById('<%= TextBoxAddDeductionsDeductWednesday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsDeductThursday') {
a = document.getElementById('<%= TextBoxAddDeductionsDeductThursday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsDeductFriday') {
a = document.getElementById('<%= TextBoxAddDeductionsDeductFriday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsDeductSaturday') {
a = document.getElementById('<%= TextBoxAddDeductionsDeductSaturday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsDeductSunday') {
a = document.getElementById('<%= TextBoxAddDeductionsDeductSunday.ClientID %>');
}
else if (next == 'TextBoxAddDeductionsDeductPPH') {
a = document.getElementById('<%= TextBoxAddDeductionsDeductPPH.ClientID %>');
}
else if (next == 'TextBoxAddMinimumHoursTuesday') {
a = document.getElementById('<%= TextBoxAddMinimumHoursTuesday.ClientID %>');
}
else if (next == 'TextBoxAddMinimumHoursWednesday') {
a = document.getElementById('<%= TextBoxAddMinimumHoursWednesday.ClientID %>');
}
else if (next == 'TextBoxAddMinimumHoursThursday') {
a = document.getElementById('<%= TextBoxAddMinimumHoursThursday.ClientID %>');
}
else if (next == 'TextBoxAddMinimumHoursFriday') {
a = document.getElementById('<%= TextBoxAddMinimumHoursFriday.ClientID %>');
}
else if (next == 'TextBoxAddMinimumHoursSaturday') {
a = document.getElementById('<%= TextBoxAddMinimumHoursSaturday.ClientID %>');
}
else if (next == 'TextBoxAddMinimumHoursSunday') {
a = document.getElementById('<%= TextBoxAddMinimumHoursSunday.ClientID %>');
}
else if (next == 'TextBoxAddMinimumHoursPPH') {
a = document.getElementById('<%= TextBoxAddMinimumHoursPPH.ClientID %>');
}
else if (next == 'TextBoxAddPayHoursTuesday') {
a = document.getElementById('<%= TextBoxAddPayHoursTuesday.ClientID %>');
}
else if (next == 'TextBoxAddPayHoursWednesday') {
a = document.getElementById('<%= TextBoxAddPayHoursWednesday.ClientID %>');
}
else if (next == 'TextBoxAddPayHoursThursday') {
a = document.getElementById('<%= TextBoxAddPayHoursThursday.ClientID %>');
}
else if (next == 'TextBoxAddPayHoursFriday') {
a = document.getElementById('<%= TextBoxAddPayHoursFriday.ClientID %>');
}
else if (next == 'TextBoxAddPayHoursSaturday') {
a = document.getElementById('<%= TextBoxAddPayHoursSaturday.ClientID %>');
}
else if (next == 'TextBoxAddPayHoursSunday') {
a = document.getElementById('<%= TextBoxAddPayHoursSunday.ClientID %>');
}
else if (next == 'TextBoxAddPayHoursPPH') {
a = document.getElementById('<%= TextBoxAddPayHoursPPH.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMinTuesday') {
a = document.getElementById('<%= TextBoxAddShiftInMinTuesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMinWednesday') {
a = document.getElementById('<%= TextBoxAddShiftInMinWednesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMinThursday') {
a = document.getElementById('<%= TextBoxAddShiftInMinThursday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMinFriday') {
a = document.getElementById('<%= TextBoxAddShiftInMinFriday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMinSaturday') {
a = document.getElementById('<%= TextBoxAddShiftInMinSaturday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMinSunday') {
a = document.getElementById('<%= TextBoxAddShiftInMinSunday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMinPPH') {
a = document.getElementById('<%= TextBoxAddShiftInMinPPH.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMaxTuesday') {
a = document.getElementById('<%= TextBoxAddShiftInMaxTuesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMaxWednesday') {
a = document.getElementById('<%= TextBoxAddShiftInMaxWednesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMaxThursday') {
a = document.getElementById('<%= TextBoxAddShiftInMaxThursday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMaxFriday') {
a = document.getElementById('<%= TextBoxAddShiftInMaxFriday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMaxSaturday') {
a = document.getElementById('<%= TextBoxAddShiftInMaxSaturday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMaxSunday') {
a = document.getElementById('<%= TextBoxAddShiftInMaxSunday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInMaxPPH') {
a = document.getElementById('<%= TextBoxAddShiftInMaxPPH.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMinTuesday') {
a = document.getElementById('<%= TextBoxAddShiftOutMinTuesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMinWednesday') {
a = document.getElementById('<%= TextBoxAddShiftOutMinWednesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMinThursday') {
a = document.getElementById('<%= TextBoxAddShiftOutMinThursday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMinFriday') {
a = document.getElementById('<%= TextBoxAddShiftOutMinFriday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMinSaturday') {
a = document.getElementById('<%= TextBoxAddShiftOutMinSaturday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMinSunday') {
a = document.getElementById('<%= TextBoxAddShiftOutMinSunday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMinPPH') {
a = document.getElementById('<%= TextBoxAddShiftOutMinPPH.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMaxTuesday') {
a = document.getElementById('<%= TextBoxAddShiftOutMaxTuesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMaxWednesday') {
a = document.getElementById('<%= TextBoxAddShiftOutMaxWednesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMaxThursday') {
a = document.getElementById('<%= TextBoxAddShiftOutMaxThursday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMaxFriday') {
a = document.getElementById('<%= TextBoxAddShiftOutMaxFriday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMaxSaturday') {
a = document.getElementById('<%= TextBoxAddShiftOutMaxSaturday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMaxSunday') {
a = document.getElementById('<%= TextBoxAddShiftOutMaxSunday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutMaxPPH') {
a = document.getElementById('<%= TextBoxAddShiftOutMaxPPH.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutRoundToTuesday') {
a = document.getElementById('<%= TextBoxAddShiftOutRoundToTuesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutRoundToWednesday') {
a = document.getElementById('<%= TextBoxAddShiftOutRoundToWednesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutRoundToThursday') {
a = document.getElementById('<%= TextBoxAddShiftOutRoundToThursday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutRoundToFriday') {
a = document.getElementById('<%= TextBoxAddShiftOutRoundToFriday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutRoundToSaturday') {
a = document.getElementById('<%= TextBoxAddShiftOutRoundToSaturday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutRoundToSunday') {
a = document.getElementById('<%= TextBoxAddShiftOutRoundToSunday.ClientID %>');
}
else if (next == 'TextBoxAddShiftOutRoundToPPH') {
a = document.getElementById('<%= TextBoxAddShiftOutRoundToPPH.ClientID %>');
}
else if (next == 'TextBoxAddShiftInRoundToTuesday') {
a = document.getElementById('<%= TextBoxAddShiftInRoundToTuesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInRoundToWednesday') {
a = document.getElementById('<%= TextBoxAddShiftInRoundToWednesday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInRoundToThursday') {
a = document.getElementById('<%= TextBoxAddShiftInRoundToThursday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInRoundToFriday') {
a = document.getElementById('<%= TextBoxAddShiftInRoundToFriday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInRoundToSaturday') {
a = document.getElementById('<%= TextBoxAddShiftInRoundToSaturday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInRoundToSunday') {
a = document.getElementById('<%= TextBoxAddShiftInRoundToSunday.ClientID %>');
}
else if (next == 'TextBoxAddShiftInRoundToPPH') {
a = document.getElementById('<%= TextBoxAddShiftInRoundToPPH.ClientID %>');
}
if (a != null) {
a.value = current.value;
}
return false;
}

推荐答案

亲爱的朋友
我认为

  最好使用切换用例(类似于C#,C ++,java),因为它可以提高可读性和可理解性.

  您也可以将功能拆分为子功能,以便更轻松地进行维护.

  如果有许多这样的javascript代码,则页面大小可能会增加并且难以管理.
  在那种情况下,最好为每个页面创建一个javascript文件(.js),以便与该页面关联的所有javascript都在一个javascript文件中,或者创建一个包含常用功能的js文件,然后参考javascript文件包含页面上必要的功能.
标签以添加js文件在下面给出

Dear Friend
In my opinion

  It whould be better to use switch cases(similar to C#, C++, java) as it increases readability and understandability.

  You can aslo split the function in to sub functions so that it would be lot more easier to maintian.

  If there are many javascript code like this the page size whould grow and will be difficult to manage.
   In that case it whould be better to create a javascript file (.js) for each page such that all javascript associated with that page is in one javascript file or create a js file that contain commonly used function then referece the javascript file the file that contains the necessary function on the page.
tag to add a js file is given below

<SCRIPT LANGUAGE="JavaScript" SRC="myscript.js">
</SCRIPT>



其中myscript.js文件包含javascript.
将以上代码段添加到html/aspx文件的head标签中.

注意:-
有时javascript文件可能不考虑服务器端脚本



Where myscript.js file contains the javascript.
Add the above snippet in the head tag of the html/aspx file.

Note :-
Some times javascript file may not consider serverside script


我要做的第一件事是将其转换为Switch大小写.

通常,我已经看到,当我们需要测试不同的变量时,我们使用if ... else if ... else;当我们要针对多个值测试单个变量时,我们使用switch().
切换将更具可读性,并且性能可能会更好.
关于Switch与If-Else-If,请阅读以下有趣的 question/discussion此处 [^ ].
First thing I will do is to convert into Switch case.

Generally, I have seen, we use if...else if...else when we need to test different variables, and we use switch() when we want to test a single variable against multiple values.

Switch would be more readable and may be better in performance.
Regarding Switch Vs If-Else-If, have a read on this interesting question/discussion here[^].


这篇关于动态查找控件-Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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