简体   繁体   English

在onclick和getvalue之后执行javascript脚本

[英]Execute javascript scripts after onclick and getvalue

I wanted to modify something in my code and don't really know how to make this work... my code is kinda huge so I am going to explain what I want with an exemple : 我想在我的代码中修改一些内容并且不知道如何使这个工作...我的代码有点大,所以我将用一个例子来解释我想要的东西:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="stylesheets/application.css">


    </head>

    <body>
      <label>Enter value : </label><input type="text" maxlength="512" id="reg_expr"/>
      <div id="button">OK</div>
        <div id="nfa"></div>

        <script src="script1.js"></script>
        <script src="script2.js"></script>
        <script src="script3.js"></script>

    </body>
</html>

So this is my HTML code, so what I want to do is NOT execute these 3 scripts until the user enters a value in the text input and clicks on OK. 所以这是我的HTML代码,所以我想要做的是在用户在文本输入中输入值并单击OK之前不执行这3个脚本。 That value will be used in the js files, so i have to get the value after I click OK. 该值将在js文件中使用,因此我必须在单击“确定”后获取该值。

Can someone explain how this has to work ? 有人可以解释这是如何工作的吗?

EDIT : problem was with jQuery that was not executing on Electron, solution : http://ourcodeworld.com/articles/read/202/how-to-include-and-use-jquery-in-electron-framework 编辑:问题是jQuery没有在Electron上执行,解决方案: http//ourcodeworld.com/articles/read/202/how-to-include-and-use-jquery-in-electron-framework

I would recommend you to load the scripts dynamically after you click on the button. 我建议您在单击按钮后动态加载脚本。 This can be done via jQuery: https://api.jquery.com/jquery.getscript/ 这可以通过jQuery来完成: https//api.jquery.com/jquery.getscript/

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="stylesheets/application.css">
        <title>NFA2DFA</title>
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    </head>
    <body>
      <label>Enter value : </label><input type="text" maxlength="512" id="reg_expr"/>
      <div id="button" onclick="loadScripts();">OK</div>
        <div id="nfa"></div>
        <script>
            function loadScripts() {
                // Is the input empty?
                var value = $("#reg_expr").val();
                if (value.length != 0) {
                    // Loads and executes the scripts
                    console.log(value); // Displays the value of the input field
                    $.getScript("script1.js");
                    $.getScript("script2.js");
                    $.getScript("script3.js");
                }
            }
        </script>
    </body>
</html>

For starters I would suggest changing; 对于初学者,我建议改变; <div id="button">OK</div> to <button id="button">OK</button> . <div id="button">OK</div><button id="button">OK</button>

I would then suggest to put each of those scripts into functions instead, then you can use the 'onClick' event from the button attribute as follows; 然后我建议将每个脚本放入函数中,然后你可以使用button属性中的'onClick'事件,如下所示;

<button id="button" onClick="s1Func();s2Func();s3Func();">OK</button>

A better way would be to have one function call 'init' or something appropriate that then calls the 3 scripts/functions and have your buttons onClick even call that one initialization function. 一个更好的方法是让一个函数调用'init'或适当的东西然后调用3个脚本/函数并让你的按钮onClick甚至调用一个初始化函数。

JSFiddle Example: https://jsfiddle.net/JokerDan/h7htk9Lp/ JSFiddle示例: https ://jsfiddle.net/JokerDan/h7htk9Lp/

You can use a button tag instead of input tag. 您可以使用按钮标记而不是输入标记。 I suggest you to use onClick event like this: 我建议你使用像这样的onClick事件:

 <button type="button" onclick="yourFunction()">Click me</button> 

When the users click the button yourFunction() is call. 当用户单击按钮时,将调用yourFunction()。

The result is this: 结果是这样的:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="stylesheets/application.css">
        <script src="script1.js"></script>
        <script src="script2.js"></script>
        <script src="script3.js"></script>

    </head>

    <body>
      <label>Enter value : </label><input type="text" maxlength="512" id="reg_expr"/>
      <button type="button" onclick="yourFunction()">Click me</button>

    </body>
</html>

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

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