2012-07-25 92 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