问题描述
我的UI页面上有四个TextField.我从所有 textfields
获取用户的 input
值,例如值 Textfield1.getvalue(),Textfield2.getvalue(),Textfield3.getvalue(),Textfield4.getvalue()
.
I have four TextFields on my UI page.I get the input
values of the user from all textfields
for example values Textfield1.getvalue(),Textfield2.getvalue(),Textfield3.getvalue(),Textfield4.getvalue()
.
现在单击按钮,我需要检查用户实际填充了哪些文本字段,并附加这些值,我需要向服务器发送http请求以在数据库上进行查询.这些值用于过滤表中的值.因此,基本上,这些值是"TABLE COLUMN"
值.为此,我想到了使用像这样的老式组合:
Now on a button click, I need to check which textfields are actually filled by the user and attach those values I need to send a http request to the server to query on the database. These values are used to filter values from a table. SO basically the values are "TABLE COLUMN"
values. For this, I thought of using old school combinations like:
if (Textfield1.getvalue()=="" && Textfield2.getvalue()!=""){
//do something
}
else if (Textfield2.getvalue()=="" && Textfield3.getvalue()!=""){
//do something
}
else if (Textfield3.getvalue()=="" && Textfield4getvalue()!=""){
//do something
}
......
and so on.
我个人认为这不是有效的,也不是一种好的编程方式.我很确定可能会有其他方式,但我并不知道,也无法找到它.任何人都可以分享一些想法以寻求更好的解决方案.
This, I personally feel is not efficient and not a good programming way. I am pretty sure there might be some other way of doing it which I am not aware of and couldnt find googling it either. Can anyone share some ideas for a better solution.
谢谢.
推荐答案
一个想法-检查单个字段一次并合并为一个唯一值:
One idea - check individual fields once and combine into a single unique value:
var c=0;
if (condition1) c+=1;
if (condition2) c+=2;
if (condition3) c+=4;
等等.现在,条件的每种组合都有一个唯一的值与之关联,您可以使用 switch
语句来实现更简洁的流程.
Etc. now every combination of conditions has a unique value associated with it and you can use a switch
statement for cleaner flow.
这篇关于如果其他条件JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!