的ReferenceError:ajaxfunction没有定义 [英] ReferenceError: ajaxfunction is not defined
问题描述
我一直无法得到一个AJAX脚本运行一段时间。
I've been unable to get an ajax script to run for some time.
基本上,我需要用户选择从一个下拉菜单中的选项,那么,根据什么选择,第二个下拉框,填入相应的基于MySQL查询。
Basically, I need the user to select an option from one drop down box, then, based on what's selected, the second drop down box with populate accordingly based on a MySQL query.
我的剧本看起来像
<script type="text/javascript">
$(function(){
$('select [name="front-size"]').change(function()
{
$.ajax({
url: '../functions/process.php',
type:'get',
data:{'value' : $(this).val()},
dataType:"html",
success: function(data) {
$("#sub").html(data);
}
});
});
});
</script>
我最初的下拉框中,填入由MySQL查询像这样
My initial drop down box is populated by a MySQL query like so
<select name="front-size" onchange="ajaxfunction(this.value)">
<?php
$door_size = $db->prepare("SELECT DISTINCT door_size FROM doors WHERE door_model = '".$_SESSION['front_door']."'");
$door_size->execute();
while($row = $door_size->fetch(PDO::FETCH_ASSOC))
{
$size = $row['door_size'];
echo '<option value="'.$size.'">'.$size.'</option>';
}
?>
</select>
第二个下拉框为空
The second drop down box is empty
<select name="front-finish" id="sub" onchange="ajaxfunction(this.value)">
</select>
和process.php应该做的依据是什么是pviously选择$ P $的下一个查询(这个工程自身)
And process.php should do the next query based on what was previously selected (this works on its own)
<?php
session_start();
include ('config.php');
$parent = $_GET['parent'];
$update_option = $db->prepare("SELECT door_finish FROM doors WHERE door_model = '".$_SESSION['front_door']."' AND door_size = '".$parent."'");
$update_option->execute();
while($row = $update_option->fetch(PDO::FETCH_ASSOC))
{
$door_finishes = $row['door_finish'];
echo '<option value="'.$door_finishes.'">'.$door_finishes.'</option>';
}
?>
在Firebug的,当我选择我的第一个下拉菜单,这个错误显示,我一直无法去解决它。
In Firebug, when I select my first drop down menu, this error is shown and I've been unable to solve it.
ReferenceError: ajaxfunction is not defined
ajaxfunction(this.value)
我该如何解决这个问题?
How can I fix this?
推荐答案
您呼叫ajaxfunction但是你有没有将其定义在code的任何地方。
you are calling ajaxfunction but you haven't defined it anywhere in the code.
<script type="text/javascript">
$(function(){
$('select [name="front-size"]').change(function()
{
$.ajax({
url: '../functions/process.php',
type:'get',
data:{'value' : $(this).val()},
dataType:"html",
success: function(data) {
$("#sub").html(data);
}
});
});
});
function ajaxFunction(stuff){
//do ajax stuff here will fix the error
}
</script>
在一个更广泛的注意事项,你为什么要调用一个内嵌在HTML(的onchange = ajaxfunction(THIS.VALUE)
)时,同样的事情可以在你准备好来完成功能?
On a broader note, why are you calling that inline in your html (onchange=ajaxfunction(this.value)
) when the same thing can be accomplished in your ready function?
<script type="text/javascript">
(function(){
$('select[name="first"]').change(function(){
//do stuff
});
$('select[name="second"]').change(function(){
//do other stuff
});
})
</script>
会更好。
编辑:检查这的jsfiddle的工作示例 http://jsfiddle.net/WF8CV/
check this jsfiddle for a working example http://jsfiddle.net/WF8CV/
这篇关于的ReferenceError:ajaxfunction没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!