使用不同的输入运行类似的函数比仅编写同一函数的略有不同版本的更好的方法?
只是想提高我的编码质量......可能是暴力强迫的 - 想知道是否有更有效的编码方法?任务:从 3 个 HTML 下拉列表中获取输入,然后传递用户输入 (m_px) 以从 googlesheet (drawx) 获取返回,然后将其写回网页。下面的工作正常...对于一个简单的任务来说似乎有很多代码/变量...我怎样才能更好地编码?例如 - 如何用单个函数替换 getDrawNumOne 到 Three?需要额外注意的是:在实际脚本中,这些函数会上升到 getDrawNumEight(!)。感谢您的建议/意见。
document.getElementById("btn").addEventListener("click",doStuff);
document.getElementById("m_p1").addEventListener("onchange",getDrawNumOne);
document.getElementById("m_p2").addEventListener("onchange",getDrawNumTwo);
document.getElementById("m_p3").addEventListener("onchange",getDrawNumThree);
function getDrawNumOne(){
var drawNumOne = document.getElementById("m_p1").value;
google.script.run.withSuccessHandler(updateDrawNumOne).getDrawNo(drawNumOne);
}
function updateDrawNumOne(drawNumReturnOne){
document.getElementById("draw1").value = drawNumReturnOne;
}
function getDrawNumTwo(){
var drawNumTwo = document.getElementById("m_p2").value;
google.script.run.withSuccessHandler(updateDrawNumTwo).getDrawNo(drawNumTwo);
}
function updateDrawNumTwo(drawNumReturnTwo){
document.getElementById("draw2").value = drawNumReturnTwo;
}
function getDrawNumThree(){
var drawNumThree = document.getElementById("m_p3").value;
google.script.run.withSuccessHandler(updateDrawNumThree).getDrawNo(drawNumThree);
}
function updateDrawNumThree(drawNumReturnThree){
document.getElementById("draw3").value = drawNumReturnThree;
}
Just trying to improve my coding quality...have probably brute forced this - wondering if there is a more efficient way to code it? The task: Am getting inputs from 3 HTML dropdowns, and then passing a user input (m_px) to get a return from a googlesheet (drawx) and then writing that back to the web page. The below works fine...just seems like a lot of code/vars for a simple task...how could I code it better? For example - how can I replace getDrawNumOne thru Three with a single function? One extra note: in the actual script these functions go up to getDrawNumEight(!). Thanks for advice/input.
document.getElementById("btn").addEventListener("click",doStuff);
document.getElementById("m_p1").addEventListener("onchange",getDrawNumOne);
document.getElementById("m_p2").addEventListener("onchange",getDrawNumTwo);
document.getElementById("m_p3").addEventListener("onchange",getDrawNumThree);
function getDrawNumOne(){
var drawNumOne = document.getElementById("m_p1").value;
google.script.run.withSuccessHandler(updateDrawNumOne).getDrawNo(drawNumOne);
}
function updateDrawNumOne(drawNumReturnOne){
document.getElementById("draw1").value = drawNumReturnOne;
}
function getDrawNumTwo(){
var drawNumTwo = document.getElementById("m_p2").value;
google.script.run.withSuccessHandler(updateDrawNumTwo).getDrawNo(drawNumTwo);
}
function updateDrawNumTwo(drawNumReturnTwo){
document.getElementById("draw2").value = drawNumReturnTwo;
}
function getDrawNumThree(){
var drawNumThree = document.getElementById("m_p3").value;
google.script.run.withSuccessHandler(updateDrawNumThree).getDrawNo(drawNumThree);
}
function updateDrawNumThree(drawNumReturnThree){
document.getElementById("draw3").value = drawNumReturnThree;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
描述
回调函数始终传递一个事件对象,而不是进行多个 onChange 回调,尽管大多数示例没有显示它。您可以检查事件对象以查看单击或更改了哪个元素,然后从那里开始。
在本例中,我使用一个
function getDrawNumber(evt){
Script
References
Description
Rather than make multiple onChange callbacks, the callback function always has an event object passed to it, although most examples don't show it. You can examine the event object to see which element was clicked or changed and go from there.
In this case I use one
function getDrawNumber(evt){
Script
References