0

此 jQuery 脚本返回 null。我已经尝试对选定的选项使用其他语法,但下面是我得到的:

该脚本运行正常,允许我下载 Excel 文件。但是 ID 未正确设置(通过选择的选项),因此解析为“0”。

<script>
//When Page Loads....
$(document).ready(function(){

  $('#dropdown').change(function(){
    // Call the function to handle the AJAX.
    // Pass the value of the text box to the function.
    sendValue($(this).val());   
  }); 
});

// Function to handle ajax.
function sendValue(str){
  // post(file, data, callback, type); (only "file" is required)
  $.post(
    "scripts/export_to_excel/index.php", //Ajax file
    { sendValue: str },  // create an object will all values
    //function that is called when server returns a value.
    function(data){
      $('#linkDiv').html(data.returnValue);
    }, 
    //How you want the data formatted when it is returned from the server.
    "json"
  );
}
</script>

选择 HTML

<p>Select event for export to Excel:</p>
<p>
  <select name="eventIDExport" id="dropdown"> 
    <option value=0> 
    <?=$options?> 
  </select>
</p>
<?php var_dump($_POST['eventIDExport']); ?>
<div id="linkDiv"></div>

渲染标记

<p>Select event for export to Excel:</p>
<p>
  <select name="eventIDExport" id="dropdown"> 
    <option value=0> 
    <option value="1">BIG event</option>
    <option value="54">2013 Network Conference</option>
  </select>
</p>
NULL
<div id="linkDiv"></div>

index.php 中用于处理 Ajax 请求的一些代码 - 我认为这是触发 null 值?

if (isset($_POST['eventIDExport']))
{
$eventIDExport = $_POST['eventIDExport'];
}else{
$eventIDExport = "";    
}
4

1 回答 1

1

为什么要发布sendValue并检查是否eventIDExport已设置?

$.post("scripts/export_to_excel/index.php", {
    sendValue: str
    ^^^^^^^^^

if (isset($_POST['eventIDExport']))
                  ^^^^^^^^^^^^^

您的代码应该是:

if(isset($_POST['sendValue']))
{
    $eventIDExport = $_POST['sendValue'];
} else {
    $eventIDExport = "";
}

或者

$.post("scripts/export_to_excel/index.php", {
    eventIDExport: str
于 2012-07-25T20:29:16.117 回答