Javascript 我尝试了jquery的反复尝试,但它没有';行不通
我用php创建了一个包含许多选择的表单。表单有3种选择:第二种选择取决于第一种选择,第三种选择取决于第二种选择。每个select都有这样一个id:type[00]、type[01]、type[02];make[00]、make[01]、make[02];型号[00]、型号[01]、型号[02] 我用。我试图编辑代码以满足我的需要,但我对java或jquery一无所知。我认为问题在于函数finishAjax,因为我不知道如何说任何select的id都是不同的Javascript 我尝试了jquery的反复尝试,但它没有';行不通,javascript,jquery,Javascript,Jquery,我用php创建了一个包含许多选择的表单。表单有3种选择:第二种选择取决于第一种选择,第三种选择取决于第二种选择。每个select都有这样一个id:type[00]、type[01]、type[02];make[00]、make[01]、make[02];型号[00]、型号[01]、型号[02] 我用。我试图编辑代码以满足我的需要,但我对java或jquery一无所知。我认为问题在于函数finishAjax,因为我不知道如何说任何select的id都是不同的 $(document).ready(f
$(document).ready(function() {
$('select[id^="type"]').change(function(){
$('select[id^="make"').fadeOut();
$.post("ajax/ajax_make.php", {
type: $('select[id^="type"]').val()
}, function(response){
setTimeout("finishAjax('make', '"+escape(response)+"')", 400);
});
return false;
});
$('select[id^="make"').change(function(){
$('select[id^="model"').fadeOut();
$.post("ajax/ajax_model.php", {
type: $('select[id^="type"]').val(),
make: $('select[id^="make"').val()
}, function(response){
setTimeout("finishAjax('model', '"+escape(response)+"')", 400);
});
return false;
});
});
function finishAjax(id, response){
$('select[id^="'+id+'"]').html(unescape(response));
$('select[id^="'+id+'"]').fadeIn();
}
A.
B
C
A.
B
C
A.
B
C
A.
B
C
$(文档).ready(函数(){
/**执行ajax请求**/
函数dispatchRequest(url、数据、超时)
{
setTimeout(函数(){
$.ajax({
类型:“POST”,
url:url,
数据:数据,
成功:finishAjax,
类型:“JSON”
});
},超时);
};
/**完成AJAX请求**/
函数finishAjax(响应)
{
/**重要的
响应现在是一个JSON对象,这只是一个简单的字符串
它代表一个复杂的对象。在这种情况下,它取决于
目标URL(ajax/ajax\u make.php)通过使用
提供的数据(id、类型)
我们至少需要三个值:
response.HTML=选择选项的HTML
response.id=所选项目的标识
response.type=选择选项的类型
**/
$(“选择[name=“+response.type+”93;+response.id)
.html(unescape(response.html))
.fadeIn();
}
/**查找每种类型的所有选择项,从而找到类选择器**/
$selectType=$(“.type”);
$selectMake=$(“.make”);
/**
更改“类型”选择选项的事件
**/
$selectType.on(“更改”,函数(){
var id=$(this.data(“id”);
/**隐藏所有“生成”选择选项**/
$selectMake.each(函数(){
$(this.fadeOut();
});
dispatchRequest(“ajax/ajax\u make.php”{
id:$(此).data(“id”),
类型:$(this.attr(“类”)
}, 1000);
返回true;
});
});
重要的变化是响应是一个JSON对象,包含多个键值对——其中一个是您需要的“id”
注意:这是未经测试的午餐时间代码。当我有机会时,我将进行测试并给出更好的解释。同时,希望代码能给你一些见解。你为什么要标记java?你是想标记java脚本吗?java与javascript无关(最初是sunScript,但后来由市场部接管:))@PermGenError我删除了java标记,因为它是irrelevant@RandomWhiteTrash:他们没有追求与JavaScript表面相似的东西,我想知道为什么…;)当我点击一个“类型”时,第二个列表被隐藏了:什么都没有发生。
<select name="type_0" data-id="0" class="type">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
<select name="type_1" data-id="1" class="type">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
<select name="make_0" data-id="0" class="make">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
<select name="make_1" data-id="1" class="make">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
<script type="text/javascript">
$(document).ready(function() {
/** Execute an ajax request **/
function dispatchRequest(url, data, timeout)
{
setTimeout(function() {
$.ajax({
type : "POST",
url : url,
data : data,
success : finishAjax,
type : "JSON"
});
}, timeout);
};
/** Finish AJAX request **/
function finishAjax(response)
{
/** IMPORTANT
response is now a JSON object, this is just a simple string
that represents a complex object. I this case, it is up to the
target URL (ajax/ajax_make.php) to create this string by using
the provided data (id,type)
At a minimum we need three values:
response.HTML = HTML of the select options
response.id = The identity of the select item
response.type = The type of select option
**/
$("select[name=" + response.type + "_" + response.id)
.html(unescape(response.html))
.fadeIn();
}
/** Find ALL the selects of each type, hence class selector **/
$selectType = $(".type");
$selectMake = $(".make");
/**
Change event for "type" select option
**/
$selectType.on("change", function(){
var id = $(this).data("id");
/** Hide all "make" select options **/
$selectMake.each(function(){
$(this).fadeOut();
});
dispatchRequest("ajax/ajax_make.php", {
id : $(this).data("id"),
type : $(this).attr("class")
}, 1000);
return true;
});
});
</script>