简体   繁体   English

我正在使用这个javascript来“未定义”。 为什么?

[英]I am geting “undefined” with this javascript. Why?

I have been digging around for 4 hours now and I decided to ask, as I can't get my head around this. 我现在已经挖了4个小时了,我决定问,因为我无法理解这个问题。

I have the following in one page: 我在一页中有以下内容:

support.php: support.php:

<div id="userID"><?= $dataOwner =></div>
<a class="nav-link" href="JavaScript:void(0);" id="showOpen">Open</a>
<script src="js/tickets.js></script>

In the above page (support.php), $dataOwner returns a value correctly. 在上面的页面(support.php)中,$ dataOwner正确返回一个值。 When clicked, the link must call the "js/tickets.js" script in which I have the following: 单击时,链接必须调用“js / tickets.js”脚本,其中包含以下内容:

tickets.js: tickets.js:

$("#showOpen").click(function(){ 
    $.ajax({
        type: "POST",

        url: 'https://[redacted]/load_tickets.php?user='+$('#userID').val(),
            success:function(data) {
                document.getElementById('openTickets').innerHTML = data;
            }
    });
});

but the value of #userID is always undefined. 但#userID的值始终未定义。 Why is this and what am I doing wrong? 为什么这样,我做错了什么?

firstly, don't use shorthand in php - might cause errors when moving servers at any point. 首先,不要在php中使用速记 - 在任何时候移动服务器时可能会导致错误。

secondly.. no inline JS!!! 其次..没有内联JS !!!

and lastly, you're using url params for a post method. 最后,你使用url params作为post方法。

How I'd do it (and how I'd recommend): 我该怎么做(以及我如何推荐):

[support] [支持]

<div id="customer" data-id="<?php echo $dataOwner; ?>">
    <?php echo $dataOwner; ?>
</div>

<a href="#" class="nav-link" id="showOpen">Open</a>

<script>
    $(document).ready(function()
    {
        $('#showOpen').on('click', function()
        {
            $.ajax({
                data: {user: $('#customer').data('id')},
                dataType: 'json',
                type: 'post',
                url: '/your/path/to/load_tickets.php',
                success: function(data)
                {
                    $('#openTickets').html(data)
                }
            });
        });
    });

then no need for tickets.js (in this case) 那么就不需要tickets.js (在这种情况下)

this will post the data to the file as $_POST (array) where you can use $_POST however you want :) 这会将数据作为$_POST (数组)发布到文件中,你可以使用$_POST但是你想要:)

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

相关问题 我正在尝试使用JavaScript创建一个计算器。 - I am trying to create a calculator with javascript. 欧拉计划 #23。 Javascript。为什么我会收到此错误 STATUS_BREAKPOINT? - Project Euler #23. Javascript. Why am I getting this error STATUS_BREAKPOINT? 为什么我得到未定义的输出Javascript? - Why am I getting undefined output Javascript? 我是 JavaScript 的新手。 请帮我进行 javascript 验证 - I am new to JavaScript. please help me for javascript validation 我正在尝试用 Javascript 制作一个计算器。 但它以某种方式不起作用 - I am trying to make a calculator in Javascript. But it is not working somehow 两个对象 javascript。 我有点困惑 - Two objects javascript. I am bit confused Ajax无法使用javascript。 我应该做些什么? - Ajax not working with javascript. What am I supposed to do? 为什么我收到TypeError:onSuccess不是我的angularjs服务中的函数 - Why am I geting TypeError: onSuccess is not a function in my angularjs service 学习JavaScript时有趣的结果。 但是我不知道为什么 - Interesting result while learning JavaScript. But I dont know why 我正在使用javascript计算页面加载时间。 但不幸的是,我得到的是负值。 - I am calculating page load time using javascript. But unfortunately I am getting it in negative value.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM