Javascript jqueryajax中下拉菜单的Onchange函数
我有一个脚本,它依赖于两个下拉列表。第三个下拉列表是价格,只读,它取决于子类别。我做了一次改变,但也有一些问题。我无法估计价格的价值。我是ajax新手,真的需要一些帮助。我得到了这个例子,并根据我的需要进行了修改。我需要从子类别的点击中获取价格 这是我的密码:Javascript jqueryajax中下拉菜单的Onchange函数,javascript,php,jquery,Javascript,Php,Jquery,我有一个脚本,它依赖于两个下拉列表。第三个下拉列表是价格,只读,它取决于子类别。我做了一次改变,但也有一些问题。我无法估计价格的价值。我是ajax新手,真的需要一些帮助。我得到了这个例子,并根据我的需要进行了修改。我需要从子类别的点击中获取价格 这是我的密码: <script> $(document).ready(function(){ var count = 0; $(document).on('click', '.add', function(){
<script>
$(document).ready(function(){
var count = 0;
$(document).on('click', '.add', function(){
count++;
var html = '';
html += '<tr>';
html += '<td> </td>';
html += '<input type="hidden" name="item_name[]" class="form-control item_name" />';
html += '<td><select name="item_category[]" class="form-control item_category" data-sub_category_id="'+count+'"><option value="">Select Service Type</option><?php echo fill_select_box($connect, "0"); ?></select></td>';
html += '<td><select name="item_sub_category[]" class="form-control item_sub_category" id="item_sub_category'+count+'"><option value="">Select Service Name</option></select></td>';
html += '<td><input type="text" name="item_sub_category_price[]" class="form-control item_sub_category_price" /></td>';
html +='<td><input type="text" name="nosessions[]" class="form-control nosessions" /></td>';
html += '<td><button type="button" name="remove" class="btn btn-danger btn-xs remove"><span class="glyphicon glyphicon-minus"></span>Remove Service</button></td>';
$('tbody').append(html);
});
$(document).on('click', '.remove', function(){
$(this).closest('tr').remove();
});
$(document).on('change', '.item_category', function(){
var category_id = $(this).find(':selected').data('foo');
var sub_category_id = $(this).data('sub_category_id');
// var sub_category_price = $(this).data('sub_category_price');
$.ajax({
url:"fill_sub_category.php",
method:"POST",
data:{category_id:category_id},
success:function(data){
var html = '<option value="">Select Sub Category</option>';
html += data;
$('#item_sub_category'+sub_category_id).html(html);
}
})
});
$(document).on('change', '.sub_category_id', function(){
alert( this.value );
});
$('#insert_form').on('submit', function(event){
event.preventDefault();
var error = '';
$('.item_category').each(function(){
var count = 1;
if($(this).val() == ''){
error += '<p>Select Item Category at '+count+' row</p>';
return false;
}
count = count + 1;
});
$('.item_sub_category').each(function(){
var count = 1;
if($(this).val() == ''){
error += '<p>Select Item Sub category '+count+' Row</p> ';
return false;
}
count = count + 1;
});
$('.item_sub_category_price').each(function(){
var count = 1;
if($(this).val() == ''){
error += '<p>Select Item price '+count+' Row</p> ';
return false;
}
count = count + 1;
});
$('.nosessions').each(function(){
var count = 1;
if($(this).val() == ''){
error += '<p>Select no of sessions '+count+' Row</p> ';
return false;
}
count = count + 1;
});
$('.packname').each(function(){
if($(this).val() == ''){
error += '<p>Select no of packname '+count+' Row</p> ';
return false;
}
});
var form_data = $(this).serialize();
if(error == ''){
$.ajax({
url:"insert.php",
method:"POST",
data:form_data,
success:function(data){
if(data == 'ok'){
$('#item_table').find('tr:gt(0)').remove();
$('#error').html('<div class="alert alert-success">Item Details Saved</div>');
}
}
});
}; else{
$('#error').html('<div class="alert alert-danger">'+error+'</div>');
}
});
});
</script>
$(文档).ready(函数(){
var计数=0;
$(文档)。在('单击','添加',函数()上){
计数++;
var html='';
html+='';
html+='';
html+='';
html+=“选择服务类型”;
html+=“选择服务名称”;
html+='';
html+='';
html+=“删除服务”;
$('tbody').append(html);
});
$(文档)。在('单击','删除',函数()上){
$(this).closest('tr').remove();
});
$(document).on('change','item_category',function(){
var category_id=$(this).find(':selected').data('foo');
var sub_category_id=$(this).data('sub_category_id');
//var sub_category_price=$(此).data('sub_category_price');
$.ajax({
url:“fill_sub_category.php”,
方法:“张贴”,
数据:{category_id:category_id},
成功:功能(数据){
var html='选择子类别';
html+=数据;
$('item_sub_category'+sub_category_id).html(html);
}
})
});
$(文档).on('change','sub_category_id',function(){
警报(该值);
});
$(“#插入表格”)。在('submit',函数(事件){
event.preventDefault();
var错误=“”;
$('.item_category')。每个(函数(){
var计数=1;
if($(this.val()=''){
错误+='在'+count+'行选择项目类别';
返回false;
}
计数=计数+1;
});
$('.item_sub_category')。每个(函数(){
var计数=1;
if($(this.val()=''){
错误+='选择项目子类别'+count+'行';
返回false;
}
计数=计数+1;
});
$('.item_sub_category_price')。每个(函数(){
var计数=1;
if($(this.val()=''){
错误+='选择项目价格'+count+'行';
返回false;
}
计数=计数+1;
});
$('.nosessions')。每个(函数(){
var计数=1;
if($(this.val()=''){
错误+='选择会话数'+count+'行';
返回false;
}
计数=计数+1;
});
$('.packname')。每个(函数(){
if($(this.val()=''){
错误+='选择packname'+count+'行的编号';
返回false;
}
});
var form_data=$(this).serialize();
如果(错误=“”){
$.ajax({
url:“insert.php”,
方法:“张贴”,
数据:表格数据,
成功:功能(数据){
如果(数据=='ok'){
$('#item_table')。查找('tr:gt(0)')。删除();
$('#error').html('项目详细信息已保存');
}
}
});
};其他{
$('#error').html(''+error+'');
}
});
});
$(文档).ready(函数(){
var计数=0;
$(文档)。在('单击','添加',函数()上){
计数++;
var html='';
html+='';
html+='';
html+='';
html+=“选择服务类型”;
html+=“选择服务名称”;
html+='';
html+='';
html+=“删除服务”;
$('tbody').append(html);
});
$(文档)。在('单击','删除',函数()上){
$(this).closest('tr').remove();
});
});
函数getsubCategory(id){
var itemcegory=“item\u category”+id;
var subcategory_id=“item_sub_category”+id;
var category_id=$(“#”+itemcegory).val();
$.ajax({
url:“fill_sub_category.php”,
方法:“张贴”,
数据:{category_id:category_id},
成功:功能(数据){
var html='选择子类别';
html+=数据;
$(“#”+子类别_id).html(html);
}
})
}
你也可以为其他人做同样的事情我假设你需要使用AJAX获取子类别的价格
$(document).on('change', '.sub_category_id', function(){
var sub_category_id = $(this).val();
$.ajax({
url:"get_sub_category_price.php",
method:"POST",
data:{sub_category_id : sub_category_id},
success:function(data){
$('#item_sub_category'+sub_category_id).parent().next().find('. item_sub_category_price').val(data);
}
});
});
get_sub_category_price.php文件应获取子类别id并回显价格。不,我没有获取价格,也没有获取子类别,我给出了这一点,但在get_sub_category_price.php中给出了什么,子类别id中是否有任何更改请告诉我如何在php文件中的价格文本框中回显,您需要获取的价格子类别,并作为ajax响应返回。比如,echo$price;您可以从数据库中获取价格或发送静态值,这取决于您。您可以发布您收到的错误消息吗?我没有收到任何错误,所以我不知道该怎么办
$(document).on('change', '.sub_category_id', function(){
var sub_category_id = $(this).val();
$.ajax({
url:"get_sub_category_price.php",
method:"POST",
data:{sub_category_id : sub_category_id},
success:function(data){
$('#item_sub_category'+sub_category_id).parent().next().find('. item_sub_category_price').val(data);
}
});
});