admin管理员组

文章数量:1430054

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. That value will be used in the js files, so i have to get the value after I click OK.

Can someone explain how this has to work ?

EDIT : problem was with jQuery that was not executing on Electron, solution :

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. That value will be used in the js files, so i have to get the value after I click OK.

Can someone explain how this has to work ?

EDIT : problem was with jQuery that was not executing on Electron, solution : http://ourcodeworld./articles/read/202/how-to-include-and-use-jquery-in-electron-framework

Share Improve this question edited Mar 5, 2017 at 13:23 Xibition asked Mar 4, 2017 at 15:37 XibitionXibition 1971 gold badge4 silver badges11 bronze badges 2
  • Check: stackoverflow./a/7789831/6695924 – kind user Commented Mar 4, 2017 at 15:40
  • 1 Possible duplicate of How to link external javascript file onclick of button – vp_arth Commented Mar 4, 2017 at 15:43
Add a ment  | 

3 Answers 3

Reset to default 2

For starters I would suggest changing; <div id="button">OK</div> to <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 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.

JSFiddle Example: https://jsfiddle/JokerDan/h7htk9Lp/

I would remend you to load the scripts dynamically after you click on the button. This can be done via jQuery: https://api.jquery./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./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>

You can use a button tag instead of input tag. I suggest you to use onClick event like this:

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

When the users click the button yourFunction() is call.

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>

本文标签: Execute javascript scripts after onclick and getvalueStack Overflow