jQuery返回null - 选择的选项没有正确发布?

时间:2012-07-25 15:31:36

标签: ajax jquery

这个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请求的一些代码 - 我认为这是触发空值吗?

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

1 个答案:

答案 0 :(得分: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