<script language='javascript' type='text/javascript' charset='<?=$page_charset?>'>
$(document).ready(function(){
$('#btn_login').click(function(){
$.search_keyword();
});
});
这是脚本
<form name='frm_search_keyword'>
<table style='width:250px;'>
<tr>
<td style='width:100px;'>
Search
</td>
<td>
<input type="text" name="web_keyword" />
</td>
<td>
<input type="submit" id="btn_login" name="btn_login" value="search!" />
</td>
</tr>
</table>
</form>
这是表格
search_keyword:function(type)
{
$.ajax({
type: 'POST',
url: '/web_keyword',
data: {'b_type':type},
cache: false,
async: false,
success: function(result){
if(result == 12001)
{
alert('please choose your interest.');
location.href = '/account/interest';
}else
location.href = '/'+type+'/'+result;
}
});
}
它成功地将"web_keyword"发送到数据库查询并得到结果。但我无法通过 ajax 脚本获取类型数据。你能帮我把数据从表单表"键入"到ajax脚本吗?
谢谢。
我不确定这是否是问题所在,但值得怀疑:
$('#btn_login').click(function(){
$.search_keyword();
});
"类型"应该从哪里来?"类型"指的是什么?"type"是从数据库查询返回的值吗?还是用户在执行搜索时选择的内容?
如果 type 是表单元素中的内容,那么使用 Javascript 或 jQuery 或其他任何东西从页面中"收获"该值,然后您需要将该数据传递给 AJAX 功能。
$('#btn_login').click(function(){
//First get the "type" value, for example if "type" is retrieved from the form element
var type = $('#btn_login').attr('type'); //this is for example's sake, since you did not assign an id to this form element...
search_keyword(type);
});
你只需要从生成和/或存储它的位置获取"类型",并将其传递给你的 AJAX 函数。
另外,也许这无关紧要,但是您的"search_keyword(("函数定义对我来说看起来很奇怪......
而不是:
search_keyword:function(type){...}
应该是:
search_keyword = function(type){...}
最后一件事,你能告诉我们你正在使用哪个Javascript库吗?
尝试添加
" dataType: 'json'," like
$.ajax({
type: 'POST',
url: '/web_keyword',
dataType: 'json',
data: {'b_type':type},
});
并返回日期
$data['return1']='true';
echo json_encode($data);
success: function(result){
if(result.return1 == 12001)
{
alert('please choose your interest.');
location.href = '/account/interest';
}else
location.href = '/'+type+'/'+result;
}
你应该删除异步:假
强烈建议不要将此选项设置为 false,因为这可能会导致浏览器无响应。