简体   繁体   English

我的onclick事件是在页面加载时执行的,而不是在单击按钮时执行的,我不知道为什么

[英]My onclick event performs when the page loads instead of when the button is clicked, and i don't know why

    function ajax_post() {

    var xmlhttp=new XMLHttpRequest();
    var working='working';
    xmlhttp.open("POST","process_form.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            // success
            var return_data = xmlhttp.responseText;
            document.getElementById("status").innerHTML = return_data;
        }
    }

xmlhttp.send(working);
alert("The ajax function is running!");
document.getElementById("status").innerHTML = "Processing. Please wait...";

}

var costButton = document.getElementById('cost-value');

costButton.onclick = ajax_post();

I'm trying to learn how to take a variable from javascript and send it to a separate php file for processing (i'm gonna put this in a database). 我正在尝试学习如何从javascript中获取变量并将其发送到单独的php文件中进行处理(我将其放入数据库中)。 My first problem seems to be that rather than the function running when i click on my cost-value button, it runs when the page loads, and the second problem appears to be that the variable i try to send (working) doesn't actually get sent. 我的第一个问题似乎是当我单击成本值按钮时函数运行,而不是在页面加载时运行,而第二个问题似乎是我尝试发送(工作)的变量实际上并没有得到发送。 This is very new to me. 这对我来说是很新的。 Any solutions? 有什么办法吗?

You're setting the onclick to the return value of the function, since you're executing the function with the parenthesis. 您正在将onclick设置为函数的返回值,因为您正在执行带有括号的函数。 Remove the () from the onclick and it will work as expected. onclick删除() ,它将按预期工作。

costButton.onclick = ajax_post; 

In order for this to work you need to make sure either: 为了使其正常工作,您需要确保:

This script appears just before the closing body tag </body> 该脚本出现在结束body标记</body>
or you wrap it in a callback for when the document is ready for JS. 或将其包装在回调中,以供文档何时可用于JS。

 document.addEventListener("DOMContentLoaded", function() {
   // Your code here
 })

暂无
暂无

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

相关问题 为什么当我的页面加载而不是单击我的单选按钮时触发我的 onchange 事件? - Why does is my onchange event triggered when my page loads instead of when I click my radio button? 为什么我的 onclick 按钮在页面加载时调用 function? - Why is my onclick button is calling a function when the page loads? 当我不单击项目时,为什么JavaScript执行“单击”事件? - Why JavaScript Performs “click” event, when I don't click on the item? 为什么我的JavaScript脚本在页面加载时运行,而不与onclick一起运行? - why is my javascript script running when the page loads and not with onclick? 页面加载时触发按钮onclick - Button onclick is fired when page loads 页面加载时会自动调用PHP函数,而不是通过onclick事件调用 - PHP function is automatically called when page loads, instead of being called through onclick event onClick 事件监听器不工作不知道为什么 - onClick event listener is not working don't know why 为什么页面加载时我的元素大小不正确? - Why aren't my elements correctly sized when the page loads? 我不知道为什么当我在我的 HTML 程序上单击“Click = Past”按钮时,绿色字符图像不显示我在可汗学院尝试过 - I don't know why When I click the button "Click = Past" on my HTML program the green character image doesn't show up I tried to this in Khan academy 如何在页面加载时停止javascript中的警报触发,并在单击按钮时使其运行? - How can I stop an alert in javascript from firing when the page loads, and make it run when a button is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM