简体   繁体   English

无法获取使用jQuery AJAX从PHP-MYSQL生成的数据的价值

[英]Unable to grab the value of data generated from PHP-MYSQL using jQuery AJAX

The title doesn't say much about the question. 标题没有对这个问题说太多。 Here is what I am trying to achieve. 这是我想要达到的目标。 I sent AJAX request using jQuery from HTML form to a php file that successfully sends back required data to AJAX. 我使用jQuery从HTML表单向php文件发送了AJAX请求,该文件成功地将所需数据发送回了AJAX。 I am also able to see this values. 我也能够看到此值。 Here is my problem: When I try to get value of a single element using their attribute, I am not able to get it. 这是我的问题:当我尝试使用它们的属性获取单个元素的值时,我无法获取它。 This will be more clear when you see my jQuery code 当您看到我的jQuery代码时,这将更加清晰

var temp_client_name;
$("#temp_client_name").on('click', 'li', function(){
    temp_client_id = $(this).attr('id');
    //temp_client_name = $("#"+temp_client_id).html();
    temp_client_name = $("li[id='+temp_client_id+']").html();
    $("#client_name").val(temp_client_name);
    console.log('temp_client_name ' + temp_client_name);
});

Now, the problem is in line 5 (alternative to line 4 which works same way I suppose and so I commented out that line as it is a good alternative to achieve same thing). 现在,问题出在第5行(替代第4行,其工作方式与我假设的相同,因此我注释掉了该行,因为它是实现相同目标的不错选择)。 In line 3, I get the ID of the HTML element whose value I am interested in and it is an attribute of li tag. 在第3行中,我获得了我感兴趣的HTML元素的ID,它是li标签的属性。 And this is also working fine, I am getting the ID of required element. 而且这也很好,我正在获取所需元素的ID。 So, where am I going wrong? 那么,我哪里出问题了?

You have to add " before and after + in : 您必须在+之前和之后添加"

temp_client_name = $("li[id='"+temp_client_id+"']").val();

But why not use : 但是为什么不使用:

temp_client_name = $(this).val();

Instead of : 代替 :

temp_client_id = $(this).attr('id');
//temp_client_name = $("#"+temp_client_id).val();
temp_client_name = $("li[id='"+temp_client_id+"']").val();

Line 线

temp_client_name = $("li[id='+temp_client_id+']").val();

means get val of li wich id is +temp_client_id+ . 使用方式取得的VAL li至极id为+temp_client_id+ If you want to use temp_client_id variable: 如果要使用temp_client_id变量:

temp_client_name = $("li[id='" + temp_client_id + "']").val();

But I don't understand why do you do this, cause accessing element by id can be done just like 但是我不明白为什么要这么做,因为按id可以访问元素就像

value = $( "#id" ).val();

you have got some mistakes in your code. 您的代码中有一些错误。

  1. The id of the elements must be unique 元素的ID必须唯一
  2. html tag <li></li> doesn't contain value attribute (so val() is not usefull here) html标记<li></li>不包含value属性(因此val()在这里没有用)

  3. this line temp_client_name = $("li[id='+temp_client_id+']").val(); 这行temp_client_name = $("li[id='+temp_client_id+']").val(); is not working at all because you have got wrong concatenation; 根本不工作,因为您的连接错误。 its must look like this $('li[id='+your_variable_here+']').val(); 它必须看起来像这样$('li[id='+your_variable_here+']').val();

  4. Check this out - https://jsfiddle.net/52hrLdkg/ 检查一下-https: //jsfiddle.net/52hrLdkg/

    var temp_client_name; $(document).on('click', 'li', function(){ temp_client_id = $(this).attr('data-id'); temp_client_name = $('#'+temp_client_id).val(); alert('temp_client_name ' + temp_client_name); });

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

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