2012-07-25 94 views
0

这个jQuery脚本返回null。我试过使用其他语法来选择选项,但这里是我得到的: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 = "";  
} 
+0

<期权价值= 0> ...这是什么? – 2012-07-25 15:39:34

+0

这是默认选项,如果没有选择它不应该发布? – 2012-07-25 15:40:00

+0

@PhilHudson你可以发布渲染的标记而不是php代码吗? – undefined 2012-07-25 15:41:52

回答

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 
+0

非常感谢你,编码的深夜对我产生了巨大的影响!非常感谢! – 2012-07-25 20:41:30