2012-09-20 65 views
1

我想要使用jQuery基于从外部PHP文件使用json_encode(数组)发送的数组来填充文本框。选择的选择框选项决定将哪个选项值发送到外部PHP文件。不幸的是,我的文本框始终是空的。我一直在试图用Firebug和lint网站进行调试,但我无法弄清楚为什么没有人口。 忽视这个安全员,请

主索引页的代码片段:

function loadDoc() 
{ 
    $('#verb').live('change', function() 
    { 
     $.post("loadTextBox.php", {verb_value: $(this).val()}, 
     function(data) 
     { 
     $("#textbox").html(data.first); 
     $("#textbox2").html(data.second); 
     $("#textbox3").html(data.third); 
     $("#textbox4").html(data.fourth); 
     $("#textbox5").html(data.fifth); 
     $("#textbox6").html(data.sitxh); 
     },"json"); 
    }); 
} 

外部PHP文件代码:

<?php 

header('Content-Type:application/json;charset=utf-8'); 

$file_absolute = "---placeholder for correct file path---"; 
include_once($file_absolute); 
$mysql = new mysqli($db_host, $db_username, $db_password, $db_name); 
$verb_value = $_POST['verb_value']; 

$mysql->query("SET CHARACTER SET 'utf8'"); 

$result = $mysql->query("SELECT present_tense FROM $verb_value"); 

$queryResult = array(); 

while ($row = $result->fetch_object()) 
{ 
    $queryResult[] = $row->present_tense; 
} 
$textboxValue = $queryResult[0]; 
$textboxValue2 = $queryResult[1]; 
$textboxValue3 = $queryResult[2]; 
$textboxValue4 = $queryResult[3]; 
$textboxValue5 = $queryResult[4]; 
$textboxValue6 = $queryResult[5]; 
$array = array('first'=>$textboxValue,'second'=> 
$textboxValue2,'third'=>$textboxValue3,'fourth'=> 
$textboxValue4,'fifth'=>$textboxValue5,'sixth'=> 
$textboxValue6); 
echo json_encode($array); 

?> 

回答

1

如果你有文本框,可以考虑使用.val()代替.html 。应该使用.val()来设置表单元素的值。

此外,.live()方法已弃用,因此请停止使用它。

从jQuery 1.7开始,不推荐使用.live()方法。使用.on()连接到事件处理程序 。老版本jQuery的用户应该优先使用 .delegate(),而不是.live()。

+0

我通过删除.live()方法并使用.val()而不是.html()修改了代码。文本框仍然没有填充。这是我的代码:http://pastebin.com/UJf57E6J – programm3r

+0

@ programm3r似乎很好。最后是'#mySelectBox'还是'#verb'?你从服务器得到什么(数据中有什么)? – kapa

+0

它应该是#verb(选择框的ID)。我编辑了上面的代码。仍然没有人满足。你如何看待数据中的内容?你可以在Firebug中看到这个,如果是,在哪里?也许有办法将什么数据记录到控制台? – programm3r

0

您正在使用jQuery的错误方法把值在文本框中

替换此

$("#textbox").html(data.first); 

随着

$("#textbox").val(data.first); 

希望这将解决您的问题。

+0

我通过删除.live()方法并使用.val()而不是.html()修改了代码。文本框仍然没有填充。这里是我的代码:pastebin.com/UJf57E6J – programm3r

+0

既然我已经接受了答案,我已经删除了Pastebin链接。 – programm3r