【发布时间】:2014-09-18 21:48:55
【问题描述】:
我有 3 个动态选择,state->country->city,我会将它们与 ajax 请求连接起来,但我做不到 :( 没有 zf2 更简单!
如果我在 stateCountryCityAction 中手动设置 $idState(例如 $idState = 1;),该函数可以正常工作,但实际上不起作用!为什么???? :(
这是我的应用程序\控制器:
public function stateCountryCityAction()
{
$form1 = new StateCountryCityForm();
$form1->get('State')->setOptions(array('value_options'=> $this->getState()));
$request = $this->getRequest();
if ($request -> isXmlHttpRequest()){
$data = $request->getPost();
if (isset($data['state'])){
$idState = $data['state'];
$form1->get('Country')->setOptions(array('value_options'=> $this->getCountry($idState)));
}
}
$viewmodel->setTerminal(true);
$viewmodel->setVariables(array(
'form1' => $form1,
));
return $viewmodel;
}
这是填充第二个选择的函数。它在我的应用程序\控制器中,位于其他功能下方:
public function getCountry($idState)
{
$dbAdapter = $this->getAdapter();
$sql = 'SELECT idCountry, Country FROM Country WHERE State_idState = '.$idState.' ORDER BY Country';
$statement = $dbAdapter->query($sql);
$result = $statement->execute();
$selectData = array('0' => '----- Tutta Italia -----');
foreach ($result as $res) {
$selectData[$res['idCountry']] = $res['Country'];
}
return $selectData;
}
这是我的 jquery 函数。我试图在选择更改时提醒 var 状态,并且输出正确! jquery 函数返回选择的值。
$("select#State").change(function () {
var state = $("select#State option:selected").attr('value');
$.post("/zf-2tutorial/public/state-country-city", { state: state }, function () {
});
});
【问题讨论】:
标签: javascript php jquery ajax zend-framework2