使用jquery和php从mysql加载数据
我有一个下拉列表DDLCount,我可以从中选择一个项目,并在test.php中执行db查询以检索相应的数据,然后用返回的数据输出一个输入元素 这是我的javascript代码:使用jquery和php从mysql加载数据,php,jquery,mysql,Php,Jquery,Mysql,我有一个下拉列表DDLCount,我可以从中选择一个项目,并在test.php中执行db查询以检索相应的数据,然后用返回的数据输出一个输入元素 这是我的javascript代码: function load1(){ $('#divAccountNum').load('test.php?accountInfo=' + document.getElementById('ddlAccount').value , '' ,function() { alert('Load was performed.')
function load1(){
$('#divAccountNum').load('test.php?accountInfo=' + document.getElementById('ddlAccount').value , '' ,function() {
alert('Load was performed.')});
}
load1函数在我更改下拉列表项时调用,它获取所选选项的值并将其发送到名为accountInfo的参数test.php中
我的html:
<select name="ddlAccount" id="ddlAccount" onchange="load1();">
<option value="1"> Account1</option>
<option value="2"> Account2</option>
<option value="3"> Account3</option>
</select>
<div id="divAccountNum" >
</div>
和testJquery.php:
if($_GET['accountInfo'])
{
$account = $accountDAO->load($_GET['accountInfo']);
$accountNum = $account->accountnumber;
echo $accountNum;
}
最后,我在divAccountNum中添加了输入元素,它的id=txtAccountNum尽管您没有提供足够的问题信息,但您可以尝试以下方法:
function load1(){
$('#ddlAccount').on('change', function() {
$.get('test.php?accountInfo=' + $(this).val(), function(response) {
$('div#divAccountNum').html(response);
}, 'html');
});
}
注:
$'DDLCount'。在“更改”上,当下拉列表更改时激发
$.get'test.php?accountInfo='+$this.val。。使用从下拉列表中选择的值向test.php发送getajax请求
带in$的参数响应。获取第二个参数回调函数是来自服务器的响应
“html”作为您返回的数据类型的$.get的第三个参数,因为您返回的是html,所以它是html。
欲了解更多信息,请阅读:
要从“选择输入”获取所选选项值,请使用:
$('#ddlAccount option:selected').val()
嗨,欢迎来到Stackoverflow。您需要告诉我们代码在做什么,看到了什么错误,以及预期的行为是什么,实际的行为是什么。告诉我们这只是一个问题,并不能让我们有很多事情要做。您可以使用问题下的编辑链接添加更多详细信息。祝你好运如果您使用jQuery,为什么不在document.getElementById'ddlAccount'中使用选择器呢?值类似于$'ddlAccount'。valI尝试了此操作,但没有发生任何问题,输入元素仍然没有出现在页面中。我使用的是Smarty。这可能是个问题吗?!不要以为聪明会引起问题。你的php脚本工作得好吗?在$'divdivAccountNum'之前使用console.logresponse调试响应。HTML响应在php中没有错误。我还回显了一个带有返回的php帐号的警报,它出现了;有回声;并从$.get中删除html
function load1(){
$('#ddlAccount').on('change', function() {
$.get('test.php?accountInfo=' + $(this).val(), function(response) {
$('div#divAccountNum').html(response);
}, 'html');
});
}
$('#ddlAccount option:selected').val()