简体   繁体   English

如何有效地实现这些 JavaScript 函数

[英]How can I implement these JavaScript functions efficiently

I've made a couple of JavaScript function and I'm looking for the best way to implement them into my web application.我已经创建了几个 JavaScript 函数,并且正在寻找将它们实现到我的 Web 应用程序中的最佳方法。

The main Three functions are:主要的三个功能是:

  • Focus()重点()

Focus does exactly what you think it will do, it gives focus to a textbox. Focus 完全按照您的想法执行,它将焦点放在文本框上。 This has a check build in that checks which textfield needs the focus.这有一个检查构建,用于检查哪个文本字段需要焦点。 The function looks something like this:该函数看起来像这样:

function Focus(field) {
            if (Locatie.value == "" && Bonregel == ""){
                Locatie.focus();
            }
            else if (Locatie.value == "") {
                Locatie.focus
            }
            else if (Bonregel.value == "") {
                Bonregel.focus();
            }
        }

And I think that I need to call use that functions as soon as the page loads.而且我认为我需要在页面加载后立即调用使用该函数。

  • Send()发送()

Send does also do what you expect it do to, it sends (submits) the data from the form. Send 也做你期望它做的事情,它发送(提交)表单中的数据。 And that functions looks something like this:这个函数看起来像这样:

function Send(keycode, locatie, bonregel) {
            //Assign fields to variables
            Form_Keycode = document.getElementById("form_keycode");
            Form_Locatie = document.getElementById("form_locatie");
            Form_Bonregel = document.getElementById("form_bonregel");
            
            // Assign values to fields
            Form_Keycode.value = keycode;
            Form_Locatie.value = locatie;
            Form_Bonregel.value = bonregel;
            
            //submit data from myform 
            document.myform.submit();
        }
  • Check查看

This is the last big function that needs to be included, Check checks if which key is pressed and then uses Send() to send the information.这是最后一个需要包含的大函数,Check 检查是否按下了哪个键,然后使用 Send() 发送信息。 This looks something like this:这看起来像这样:

function Check() {
            // check alle data vooor verzenden
            // Keycode 13 -> ENTER
            // Keycode 125 -> Green key on device

            Locatie = document.getElementById("locatie").value;
            Bonregel = document.getElementById("bonregel").value;

            if (event.keyCode == 125) {
                Send(125, locatie, bonregel);
            }
            else if (event.keyCode == 13) {
                delay(Send(13,locatie,bonregel), "Send", 500);
            }
            else {
                Focus();
            }
        }

Now on to the question:现在来回答这个问题:

What would be the most efficient way to implement these functions into my html, and which methods should be used?将这些功能实现到我的 html 中的最有效方法是什么,应该使用哪些方法? my html looks like this:我的 html 看起来像这样:

<legend>Test</legend><br />
            <form name="myform" action="/" method="post">
                <input type="hidden" id="form_locatie" name="locatie" />
                <input type="hidden" id="form_bonregel" name="bonregel" />
                <input type="hidden" id="form_keycode" name="keycode"/>
            </form>
            <table>
                <tr>
                    <td>Locatie:</td>
                    <td><input type="text" id="locatie" onkeyup="CheckLocation()" value="{locatie}" /></td>
                </tr>
                <tr>
                    <td>Bonregel:</td>
                    <td><input type="text" id="bonregel" onkeyup="keyup(event)" /></td>
                </tr>
                <tr>
                    <td>Bonlijst:</td>
                    <td><textarea id="bonregelbox" readonly></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="Velden Legen" id="reset" onclick="ClearFields()" /></td>
                </tr>
            </table>

I would not use a different form for a function which requires javascript.对于需要 javascript 的函数,我不会使用不同的形式。 I would wrap the table with a form element, add validation on event on the input fields onchange="check(this)" and keep it simple.我会用表单元素包装表格,在输入字段onchange="check(this)"上添加事件验证并保持简单。

<form name="myform" action="/" method="post" onsubmit='return validate();'>
        <table>
            <tr>
                <td>Locatie:</td>
                <td><input type="text" id="locatie" onchange="check(this)" value="{locatie}" /></td>
            </tr>
            <tr>
                <td>Bonregel:</td>
                <td><input type="text" id="bonregel" onchange="check(this)" /></td>
            </tr>
            <tr>
                <td>Bonlijst:</td>
                <td><textarea id="bonregelbox" readonly></textarea></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" value="Velden Legen" id="reset" onclick="ClearFields()" /></td><!-- i do not understand this! -->
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
        </form>

This avoid's unnecessary assignments of values and variable declarations这避免了不必要的值赋值和变量声明

1) Focus(param1,param2) 1)焦点(参数1,参数2)

function Focus(Locatie,Bonregel) {
            if (Locatie.value == "" && Bonregel.value  == ""){
                Locatie.focus();
            }
            else if (Locatie.value == "") {
                Locatie.focus
            }
            else if (Bonregel.value == "") {
                Bonregel.focus();
            }
        }

2) Send(param1,param2,param3) 2) 发送(param1,param2,param3)

function Send(keycode, locatie, bonregel) {
            //Assign value directly
            document.getElementById("form_keycode").value = keycode;
            document.getElementById("form_locatie").value=locatie;
            document.getElementById("form_bonregel").value=bonregel;
            //submit data from myform 
            document.myform.submit();
        }

3) Check() 3)检查()

function Check() {
            // check alle data vooor verzenden
            // Keycode 13 -> ENTER
            // Keycode 125 -> Green key on device
            if (event.keyCode == 125) {
                Send(125, document.getElementById("locatie").value, document.getElementById("bonregel").value);
            }
            else if (event.keyCode == 13) {
                delay(Send(13,document.getElementById("locatie").value,document.getElementById("bonregel").value), "Send", 500);
            }
            else {
                Focus(document.getElementById("locatie"),document.getElementById("bonregel"));
            }
        }

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

相关问题 如何在 JavaScript 中实现一组函数? - How can I implement an array of functions in JavaScript? 如何更有效地在 JavaScript 中实现这些基数到字符串函数? - How to more efficiently implement these radix to string functions in JavaScript? 如何有效地使用firebase功能? - How can I use firebase functions efficiently? 如何有效地实现客户端javascript模板局部函数? - How to efficiently implement clientside javascript templating partials? 如何有效地验证 Javascript 函数中的所有参数? - How to efficiently validate all args in Javascript functions? 如何为 3 个函数实现 Javascript 承诺? - How to implement Javascript promises for 3 functions? 如何有效地将网络流缓冲到JavaScript中的文件? - How can I efficiently buffer a network stream to a file in javascript? Javascript + HTML:如何有效显示大量图像? - Javascript + HTML: How can I efficiently display a large list of images? 如何有效地将css值导入javascript文件? - How can I efficiently import css values into javascript file? 如何以不同的方式有效地对 Javascript 中的对象数组进行排序? - How can I efficiently sort an array of objects in Javascript in different ways?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM