You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

xajax中传递getElementByID获取的含空格值失效问题求助

问题描述

在textarea中输入单个单词时,Xajax代码能正常弹出内容;但输入多个单词时,Xajax无响应。

初始代码

<?php
   require_once("xajax/xajax_core/xajaxAIO.inc.php");
   $xajax = new xajax();

   function testEE($datos){
      $x = $datos;
      $rr = new xajaxResponse();
      $rr->alert($x);
      return $rr;
   }
   
   $xajax->registerFunction("testEE");
   $xajax->processRequest();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test E</title>
<?php $xajax->printJavascript("xajax/"); ?>
</head><body>
<div id="display" style="width:750px; margin-right:auto; margin-left:auto; margin-top:100px;">
   <form id='forma' name='forma'>
      <table cellpadding='8' style='float:left;'>
         <tr><td>Pregunta:</td><td><textarea name='question1' id='question1' cols='40' rows='8'></textarea></td></tr>
         <tr><td colspan='2'><input type='button' value='Agregar' onclick="xajax_testEE(document.getElementById('question1').value)"></td></tr>
      </table>
   </form>
</div>
</body>
</html>

现象:输入单个单词时正常弹出内容,输入多个单词无反应。

尝试的代码

<?php
   require_once("xajax/xajax_core/xajaxAIO.inc.php");
   $xajax = new xajax();

   function testEE($datos){
      $x = $datos;
      $rr = new xajaxResponse();
      $rr->alert($x);
      return $rr;
   }
   
   $xajax->registerFunction("testEE");
   $xajax->processRequest();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test E</title>
<?php $xajax->printJavascript("xajax/"); ?>
</head><body>
<div id="contenido">
   <div id="display" style="width:750px; margin-right:auto; margin-left:auto; margin-top:100px;">
      <form id='forma' name='forma'>
         <table cellpadding='8' style='float:left;'>
            <tr><td>Pregunta:</td><td><textarea name='question1' id='question1' cols='40' rows='8'></textarea></td></tr>
            <tr><td>Respuesta 1:</td><td><textarea name='respuesta1' id='respuesta1' cols='40' rows='8'></textarea></td></tr>
            <tr><td colspan='2'><input type='button' value='Agregar' onclick="myFunction()"></td></tr>
         </table>
      </form>
   </div>
</div>

<script>
function myFunction() {
  var x = document.getElementById("question1").value;
  xajax_testEE(x);
  alert('myFunction alert: ' + x);
}
</script>
</body>
</html>

现象:输入单个单词时,能收到myFunction和Xajax的两个弹窗;输入多个单词时,仅收到myFunction的弹窗,Xajax的弹窗不出现。

解决方案

问题核心是带空格的字符串在Xajax参数传递时未被正确处理,以下两种方法可解决:

方法1:手动编码参数

调用Xajax函数时,用encodeURIComponent对参数编码,PHP端再解码:

// 修改myFunction中的调用代码
xajax_testEE(encodeURIComponent(x));
// 修改PHP端testEE函数
function testEE($datos){
   $x = urldecode($datos);
   $rr = new xajaxResponse();
   $rr->alert($x);
   return $rr;
}

方法2:使用Xajax数组参数传递

Xajax支持数组形式传参,会自动处理特殊字符和空格,无需修改PHP代码:

// 修改myFunction中的调用代码
xajax_testEE([x]);

额外修复

尝试的代码中alert('myFunction alert': x)存在语法错误,应修正为:

alert('myFunction alert: ' + x);

内容的提问来源于stack exchange,提问作者Enrique Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 20:35:17