简体   繁体   English

html元素的value属性未定义

[英]Value attribute of html element is undefined

Why is name undefined? 为什么name未定义?

$('#langs li').click(function(){
    var name = $(this).attr('value');
    $.ajax({
        type: "POST",
        url:'test.php',
        data: 'name='+name,
        success:function(raspuns){
            //$('#content1').html(raspuns);
            var ras = raspuns;
            $.ajax({
                type: "POST",
                url: "index.php",
                data: 'ras='+ras;
            )};
        }
    });
});

If what you mean is that raspuns seems to be undefined, maybe it's because you did not echo your response from test.php ? 如果你的意思是, raspuns似乎是不确定的,也许是因为你没有echo从你的回应test.php

test.php test.php的

...
echo 'this is my response';

AJAX call AJAX调用

$.ajax({
  ...
  success: function(raspuns) {
    // raspuns == 'this is my response'
  }
});

And also, if you're passing POST data, I think it would be better if you pass a JSON object, like so: 而且,如果您传递POST数据,我认为如果传递JSON对象会更好,如下所示:

$.ajax({
  url: 'test.php',
  type: 'POST',
  data: {name: name},
  ...
});

You can check a few things: make sure you have data before sending. 您可以检查以下几点:确保在发送之前有数据。 you have value attribute on li? 你有李的价值属性吗? or if you want to get li contents, use html() or txt(). 或者如果你想获得li内容,请使用html()或txt()。 But probably you want to get input field value inside li?. 但是你可能想在li里面获得输入字段值? then use $(this).find("input").val() if you have just one input inside. 然后使用$(this).find(“input”)。val()如果你只有一个输入。 Then others to check: 然后其他人检查:

1) Visit http://example.com/test.php to make sure it echoes the response correctly. 1)访问http://example.com/test.php以确保它正确回应响应。 You may have error in php or the link may not be accessible. 您可能在php中有错误或链接可能无法访问。

2) Your url is like this: http://example.com/test.php ? 2)你的网址是这样的: http//example.com/test.php It is also fine if you have a virtual host in your local machine like http://example.local/test.php . 如果您的本地计算机中有虚拟主机,例如http://example.local/test.php,那也没关系。 But it will not work if you have something like 但如果你有类似的东西,它就行不通

http://localhost/mysite/test.php 

unless you correct your path in ajax call to a full link. 除非你在ajax调用完整链接中纠正你的路径。

3) Make sure your javascript doesnt fail before sending. 3)确保你的javascript在发送之前没有失败。 I mean, are you able to do alert(name) ? 我的意思是,你能做警报(姓名)吗? You can also use beforeSend() above success to check if you are ending data correctly. 您还可以在成功之上使用beforeSend()来检查是否正确结束数据。

4) Make sure you are not trying to make a cross domain ajax request as you can't do so with POST. 4)确保您没有尝试制作跨域ajax请求,因为您无法使用POST执行此操作。

5) May try using "/test.php" instead of "test.php" although it wouldn't be the problem, I think. 5)我认为可以尝试使用“/test.php”而不是“test.php”,尽管这不是问题。

You can also use console to see what is going on. 您还可以使用控制台查看发生了什么。

li elements don't support a value attribute. li元素不支持value属性。 Perhaps you're looking for an input or the contents of li via .html() . 也许你正在通过.html()寻找输入或li的内容。

See in this demo that name is undefined: http://jsbin.com/IzOXiJOZ/2/edit 请参阅此演示,该name未定义: http//jsbin.com/IzOXiJOZ/2/edit

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

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