简体   繁体   English

仅在创建第一个Javascript函数中创建的表单之后,才调用第二个Javascript函数

[英]Call second Javascript function only after the form created in the first Javascript Function has been created

I am attempting to create an application with dynamic forms. 我试图用动态表格创建一个应用程序。 I would like on-screen calculations for these forms but I am struggling. 我想在屏幕上计算这些表格,但是我很挣扎。 I can get the JS to work when it is a static webpage and now I'm attempting to get that logic working for a dynamic page. 当它是静态网页时,我可以使JS工作,现在我试图使该逻辑适用于动态页面。

I believe what I am after is for the on-screen calculations JS function only to be called after the form has been created. 我相信我所追求的是仅在创建表单后才调用屏幕计算JS函数。 However, I may just not understand the basis of HTML / JS - I'll let you decide that... 但是,我可能只是不了解HTML / JS的基础-我会让您决定...

The original page that when the drop down is changed the "form" is created (Basically - On drop down change, load Page2.php into div "ForecastForm", then using the input boxes & divs in Page2.php, the 2nd JS function aims to provide on-screen calculations): 更改下拉菜单时将创建“表单”的原始页面(基本上-在下拉菜单中,将Page2.php加载到div“ ForecastForm”中,然后使用第二个JS函数Page2.php中的输入框和divs旨在提供屏幕计算):

<div>Top</div>
<br></br>
<script>
function GetForm(str) {
if (str == "") {
    document.getElementById("ForecastForm").innerHTML = "";
    return;
} else { 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("ForecastForm").innerHTML = this.responseText;
        }
    };
    xmlhttp.open("GET","Page2.php?text="+str,true);
    xmlhttp.send();
}
}
</script>

<select id="SiteDD" onchange="GetForm(this.value)">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<div id="ForecastForm"><b></b></div>

<script type="text/javascript">
for(var x=1; x<3; x++){

let num = "num"+x;
let input = "input"+x;
let total = "total"+x;
let element = "element"+x;
let inputelement = "inputelement"+x;
let thesum = "thesum"+x;

element = document.getElementById(num),
inputelement = document.getElementById(input),
thesum = document.getElementById(total);
document.getElementById(input).onchange = function() {
thesum.innerHTML = parseInt(element.innerHTML) + parseInt(inputelement.value);
};
};
</script>

<br></br>

<div>Bottom</div>

and the "Form" that gets loaded: 和加载的“表格”:

<div id="num1">11</div>

<input type=number value=2 id="input1">

<div id="total1">0</div>

<br>
<br>

<div id="num2">11</div>

<input type=number value=2 id="input2">

<div id="total2">0</div>

Any help or advice would be greatly appreciated. 任何帮助或建议,将不胜感激。 Thank you. 谢谢。

You are trying to bind event handlers before the dynamic content is loaded, so no num1 , num2 , nor num3 elements will be found during the execution of your for loop (the same happens for the other elements). 您正在尝试在加载动态内容之前绑定事件处理程序,因此在执行for循环期间不会找到num1num2num3元素(其他元素也是如此)。

Instead, use the principle of event delegation: the change event will bubble up, so although the triggering input element is not yet there, you can already listen to the change event on the document. 而是使用事件委托的原理: change事件将冒泡,因此尽管触发input元素尚不存在,但您已经可以在文档上侦听change事件。 When later the input is added to the document, and generates the change event, your event handler will capture that event as it bubbles up the DOM hierarchy. 当稍后将input添加到文档中并生成change事件时,事件处理程序会在DOM层次结构冒泡时捕获该事件。

Instead of your for loop, you could use code along these lines: 除了使用for循环外,还可以使用以下代码:

function calculate() {
    document.getElementById('total' + SiteDD.value).textContent =
        +document.getElementById('num' + SiteDD.value).textContent +
        +document.getElementById('input' + SiteDD.value).value;
}

document.onchange = function(e) {
    // Check if this event was triggered from the input element we want to
    // listen to. If so, call the calculate function:
    if (e.target.id === 'input' + SiteDD.value) calculate();
};

You could also call the calculate function as soon as the dynamic content has been injected: 您还可以在注入动态内容后立即调用calculate函数:

    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            ForecastForm.innerHTML = this.responseText;
            calculate(); // <---
        }
    };

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

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