简体   繁体   English

Select Box onchange事件的ajax代码

[英]ajax code for Select Box onchange event

I have select box with menu(id,name) and I have another select box category (cid, cname) and it must show only category based on the menu selected by setting the menuid to " url ". 我有带有menu(id,name)的选择框,并且我还有另一个选择框类别(cid, cname)并且它必须仅根据将menuid设置为“ url ”而选择的菜单显示类别。 Here is my code: http://jsfiddle.net/L8su2/738/ 这是我的代码: http : //jsfiddle.net/L8su2/738/

echo $this->Form->create('Subcategorynew');
echo $this->Form->input('menu_id', array('empty'=>'--Select--','label'=>'Menu','type'=>'select','options'=>$menunew, 'div' => false, 'id' => 'prodid', 'onchange' => 'test()', 'class' => 'form-control'));
echo "</br>";
echo $this->Form->input('category_id', array('type'=>'select','label'=>'Category', 'div' => false, 'id' => 'total','options'=>$catnew, 'class' => 'form-control'));
echo "</br>";

First, you should add an error trigger to see what happens. 首先,您应该添加一个错误触发器以查看会发生什么。 AJAX is based on JSON and you are sending HTML code. AJAX基于JSON,您正在发送HTML代码。 Then you HAVE to change the data type. 然后,您必须更改数据类型。 Finally, to do it easier, you can use $.get() and $.post() to be sure the way data is coming. 最后,为了简化操作,可以使用$ .get()和$ .post()来确定数据的发送方式。

error: console.log,
dataType : 'html',

It should be something like this to test. 要进行测试。 Results are in your developpement console like in Chrome or Firefox 结果在您的开发控制台中,例如在Chrome或Firefox中

$.post({
   url: "<?=$this->webroot?>admin/subcategorynew/add/"+prodid,
   data: data,
   error: console.log,
   success: console.log,
   dataType: 'html'
});

or 要么

$.get({
   url: "<?=$this->webroot?>admin/subcategorynew/add/"+prodid,
   data: data,
   error: console.log,
   success: console.log,
   dataType: 'html'
});

To display data where we want, via JQuery 通过JQuery在所需的位置显示数据

Set the callback 设置回调

success: dataToProcess,

Create the callback 创建回调

function dataToProcess(data, status){
  // select your submenu via the ID
  // $.html() will replace the current HTML data INTO the actual tag to the data you sended
  $("#idSubMenu").html(data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM