简体   繁体   English

我如何将 AJAX (JS) 值传递给这个?

[英]How do I pass AJAX (JS) value to this?

I'm trying for hours to do this simple thing but I don't seem to be able to.我花了几个小时来做​​这件简单的事情,但我似乎做不到。 I am familiar quite a bit with PHP but JS and AJAX are alien to me.我对 PHP 相当熟悉,但 JS 和 AJAX 对我来说很陌生。

So, what I'm trying to do is to pass simple value from form that I have.所以,我想要做的是从我拥有的表单中传递简单的值。 But first things first.但首先。

I am unable even to pass value here:我什至无法在这里传递价值:

 function load_data(query)
 {
    var simple = "123";
  $.ajax({
   url:"pool/"+simple,
   data:{query:query},
   success:function(data){
    $('#result').html(data);
   }
  })
 }

How to get this done, even this simple part first?如何完成这项工作,即使是先完成这个简单的部分? Have "simple" actually call有“简单”的实际调用

url:"pool/123",

If ever manage to do this I have a bit more complex question to answer.如果曾经设法做到这一点,我有一个更复杂的问题要回答。 But I am even not able to do this... What I'm doing wrong here?但我什至无法做到这一点......我在这里做错了什么?

Everything else I managed to do, now I need page to be fetched by IDs and that would be like this simple 123 or I'll explain later how I can get them from "data-" values, but how to achieve this?我设法做的所有其他事情,现在我需要通过 ID 获取页面,这就像这个简单的 123 或者我稍后会解释如何从“数据-”值中获取它们,但是如何实现呢? What I'm doing wrong here.我在这里做错了什么。

EDIT: Just to be clear, I'm trying just to pass value of variable here just as I'd do in PHP so that it can be "called" like that (fetch that URL).编辑:为了清楚起见,我只是想在这里传递变量的值,就像我在 PHP 中所做的那样,以便它可以像那样“调用”(获取该 URL)。 For example I'm trying to achieve this (sample in PHP below)例如,我正在尝试实现这一点(下面的 PHP 示例)

<?php $simpleSample = "123"; ?>
<script>
function load_data(query)
     {
      
      $.ajax({
       url:"pool/<?php echo $simpleSample; ?>",
       data:{query:query},
       success:function(data){
        $('#result').html(data);
       }
      })
     }
</script>

EDIT 2: I managed to get somewhere... I think.编辑 2:我设法到达某个地方......我想。

When I do now:当我现在这样做时:

console.log($("#myFormName [name='simple']").val());

I get two values as result (so this is all good):我得到两个值作为结果(所以这一切都很好):

1212 1212

undefined不明确的

What I'm thinking is problem now is somehow with defining var as I do it like this:我现在在想的问题是在某种程度上定义 var ,因为我这样做:

var simple = $("#myFormName [name='simple']").val();

Yet it still does not work.然而它仍然不起作用。 So, what I'm doing wrong to pass that value to url: ?那么,将该值传递给 url: 时我做错了什么?

Everything was working exactly as expected.一切都按预期工作。 So code above works just fine.所以上面的代码工作得很好。

Problem was that I did testing for half a day now, when I searched for results it was inside of category that has no results (hence, no reply from server).问题是我现在测试了半天,当我搜索结果时,它在没有结果的类别内(因此,服务器没有回复)。

Anyway, it all works just with the code above.无论如何,这一切都只适用于上面的代码。

Thanks to all who wanted to help.感谢所有想提供帮助的人。

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

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