【问题标题】:How actual parameter can be passed here?这里如何传递实际参数?
【发布时间】:2019-01-27 14:25:40
【问题描述】:

JS函数不能带参数调用

我已经通过onclick事件将实际参数从php文件传递到div,它在页面上呈现,函数本身存在,但参数不传递给函数调用

形式参数与函数定义中的一样,它不会被传递给 onclick 事件的内容所取代

http://localhost:8000/sknt1.php?reqStr

代码本身如下所示:

for ($i=0; $i<count($json_a["tarifs"]) ; $i++) {
 echo "<div onclick='fun(".$i.")' class='column' style='font-size:20px; width:100%'>

在页面上看起来像这样(0 到 4):

<div onclick="fun(0)" class="column" style="font-size:20px; width:100%">
<div onclick="fun(1)" class="column" style="font-size:20px; width:100%">

等等。

fun() 是一个 AJAX 调用函数,它是一次写入页面,我无法将它嵌入到 php for 循环中:

 <script type='text/javascript'>
function fun(reqStr) {
var xmlhttp = null;

function AjaxRequest(url){
  if(xmlhttp != null){
    if(xmlhttp.abort)
      xmlhttp.abort();
    xmlhttp = null;
  };
  if(window.XMLHttpRequest) // good browsers
    xmlhttp=new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');

  if(xmlhttp == null)
    return null;

  xmlhttp.open('GET',url,false);
  xmlhttp.send(null);

  if(xmlhttp.status >= 200 && xmlhttp.status < 300)// 2xx is good enough
    return xmlhttp.responseText;
  else
    return null;
}
window.history.replaceState({}, '',`/sknt1.php?reqStr`);
var url = '/sknt1.php?reqStr';
  var contents = AjaxRequest(url);
  if(contents){
    document.documentElement.innerHTML = contents;}
}
</script>

我想要这个 Ajax 调用来替换传入的变量并替换 url 为 smth。比如http://localhost:8000/sknt1.php?0,多了个php文件,操作历史后会渲染新页面,请帮忙

【问题讨论】:

  • Ajax 调用什么?
  • 您在页面源代码中的 JavaScript 中看到您的 PHP 输出了吗?如果是这样,PHP 部分是无关紧要的,你应该发布 that 而不是这段代码。
  • 其他人看不到您的“本地主机” - 发布代码和结果
  • 有什么反馈吗?我明天需要提交我的代码,不明白为什么在一般功能中没有替换实际参数。任何帮助将不胜感激,伙计们。

标签: javascript php scope


【解决方案1】:

只需在 for 循环中创建一个变量 $iStr = ' " '.$i.' " ' 并传递新变量。

【讨论】:

  • 应该是通用函数,只有for循环内的onclick调用会有所不同
【解决方案2】:

这是一个菜鸟问题。显然我应该串联参数[0],而不是直接传递它。

不是这个/sknt1.php?reqStr,而是这个/sknt1.php?+reqStr

【讨论】:

    猜你喜欢
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多