简体   繁体   English

无法触发刷新javascript的一部分

[英]Can't trigger refresh of just one portion of javascript

The other answers to this question deal with timing whereas with this problem deals with refreshing a portion of javascript within an if statement. 该问题的其他答案涉及计时,而此问题涉及在if语句中刷新部分javascript。

Once the HTML page loads there is one select element visible. HTML页面加载后,将显示一个选择元素。 Depending on which option is selected a different set of input fields will appear. 根据选择的选项,将显示一组不同的输入字段。

My initial problem was that when the user reclicks the button the function I made 'Submit()' will redraw the same input fields over the old ones causing a mess. 我的最初问题是,当用户重新单击按钮时,我做的'Submit()'函数将在旧的输入字段上重绘相同的输入字段,从而导致混乱。 You see the same input fields being drawn as many times as the user clicks "submit". 您看到与用户单击“提交”一样多次绘制相同的输入字段。

I added a function called checkBool() to check whether the form has been drawn or not. 我添加了一个名为checkBool()的函数来检查表单是否已绘制。 If it has, then it'll call the window.location.reload() function. 如果有的话,它将调用window.location.reload()函数。 if not, it'll just call my Submit() function. 如果没有,它将仅调用我的Submit()函数。

The code now won't pile input fields anymore. 该代码现在将不再堆积输入字段。 Instead when I hit the submit button a 2nd time the drawn forms will disappear, even if a different option is selected. 相反,当我第二次点击提交按钮时,即使选择了其他选项,绘制的表单也会消失。

What I want to happen is when the user chooses a different option and hit's submit, the present form is removed and the new set of forms are automatically drawn. 我想发生的是,当用户选择其他选项并点击提交时,当前表单被删除,新的表单集被自动绘制。

Here is my checkBool() function: 这是我的checkBool()函数:

var boolean = new Boolean();
function checkBool(){

    if(boolean==false){
        Submit();
    }else if(boolean==true){
        window.location.reload();
        boolean=0;
        Submit();
        return boolean;
    }
}

It seems that the window.location.reload() function immediately reloads the script and the browser never executes the "Submit()" function. 似乎window.location.reload()函数会立即重新加载脚本,并且浏览器永远不会执行“ Submit()”函数。

My choice would be to draw the different forms to different divs, with initially none of them shown. 我的选择是将不同的形式绘制到不同的div,最初没有显示。 The onchange event of the select box or the submit button cycles through this divs, setting .style.visibility='hidden' or 'visible' depending on whether this is the requested form or not. 选择框或提交按钮的onchange事件在此div中循环,根据是否为所请求的表单设置.style.visibility ='hidden'或'visible'。

This has also the usability advantage, that if a user changes his mind after starting to fill out a form, later changes his mind back, his input will still be there. 这也具有可用性优势,即如果用户在开始填写表单后改变主意,稍后改变主意,他的输入仍将存在。

I found the answer to my question. 我找到了问题的答案。

Instead of refreshing the javascript I can remove the elements all together. 我可以将所有元素一起删除,而不是刷新javascript。 This is what I was attempting to do by finding a way to 'refresh' the page. 这是我试图通过找到“刷新”页面的方法来做的事情。

Instead of hiding the elements and leaving a huge gap in different spots I can strategically place the elements within divs and use the removeChild() function. 我可以策略性地将元素放在div中并使用removeChild()函数,而不是隐藏元素并在不同的位置留下巨大的空白。

Here is an example of how it works... 这是一个工作原理的例子...

This is my html: 这是我的HTML:

<button type="button" onclick="draw()">Draw</button>
<button type="button" onclick="erase()">Erase</button>
<div id="parent">

</div>

Here is my javascript: 这是我的javascript:

    function draw(){
     `docume`nt.getElementById("test").innerHTML=InputField("parent","child","text",5,10).value;
}

function erase(){
    var parent =document.getElementById("parent");
    var child = document.getElementById("child");
    parent.removeChild(child);


}

So what happens here is when the user clicks the button "draw" the draw() function is called. 所以这里发生的是当用户点击按钮“draw”时调用draw()函数。 This function creates an input field. 此函数创建一个输入字段。

I had created my own function that would draw an input field with the variable I passed it. 我创建了自己的函数,它将使用我传递的变量绘制一个输入字段。 That's what the InputField() function is all about. 这就是InputField()函数的全部内容。

After that the user clicks the 'erase' button to call the erase function. 之后,用户单击“擦除”按钮以调用擦除功能。

The erase function grabs the parent element named 'parent' and the child element (which in this case is called 'child' - you can see I passed the name 'child' into my InputField() function) 擦除函数抓取名为'parent'的父元素和子元素(在本例中称为'child' - 你可以看到我将名称'child'传递给我的InputField()函数)

Then remove the child element with the remove.Child(child) portion of the code. 然后使用代码的remove.Child(child)部分删除child元素。

If you download jquery into your webpage you can use the removeDiv() function to do the same thing. 如果将jquery下载到您的网页中,则可以使用removeDiv()函数执行相同的操作。

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

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