[英]Run jquery function when click on bootstrap tab
I want to run an ajax function when I click on bootstrap tab. 我想在单击bootstrap选项卡时运行ajax函数。
I have html: 我有html:
<li><a href="#upotrebljeni" data-toggle="tab">Upotrebljeni resursi</a></li>
jquery ajax function: jquery ajax函数:
$('a #upotrebljeni').click(function (e) {
var ajdi = '22';
var radnici = 'radnici';
var prvi = '1';
$.ajax({
url: 'getRMZ.php', // make this url point to the data file
dataType: 'json',
data:{id_akt:ajdi, tabela:radnici, prvi:prvi},
async: false,
type: 'POST',
success:function(data){
console.log(data);
},
error:function(data) {
console.log(data);
}
});
});
and getRMZ.php pdo file: 和getRMZ.php pdo文件:
try {
/* Establish the database connection */
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $conn->query("SELECT id_tabele,naziv FROM track_aktivnosti WHERE prvi = :prvi AND id_akt = :id_akt AND tabela = :tabela");
$result->execute(array(':prvi' => $_POST['prvi'], ':id_akt' => $_POST['id_akt'], ':tabela' => $_POST['tabela']));
$jsonTable = json_encode($result);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo $jsonTable;
Now when I click on tab a #upotrebljeni I dont get any action, any error, nothing ... What can be a problem here? 现在,当我点击标签#upotrebljeni时,我没有得到任何动作,任何错误,没有......这里有什么问题?
UPDATE: I via ajax get an error: "ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':prvi AND id_akt = :id_akt AND tabela = :tabela' at line 1" 更新:我通过ajax得到一个错误: “错误:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法':prvi AND id_akt =:id_akt AND tabela =:tabela'at line 1“
<a id="upotrebljeni" href="#upotrebljeni" data-toggle="tab">Upotrebljeni resursi</a>
Remove the space. 删除空间。
Use 采用
$('a#upotrebljeni').click(function (e) {
instead of 代替
$('a #upotrebljeni').click(function (e) {
^
The a #upotrebljeni
selector will look for an element with the ID inside an <a>
tag, while the a#upotrebljeni
selector looks for an <a>
element that has the ID. a #upotrebljeni
选择器将在 <a>
标签内查找具有ID的元素,而a#upotrebljeni
选择器查找具有ID的<a>
元素。
You need to add id="upotrebljeni"
to your anchor: 你需要在你的锚点添加
id="upotrebljeni"
:
<li><a id="upotrebljeni" href="#upotrebljeni" data-toggle="tab">Upotrebljeni resursi</a></li>
since you're using #
so it'll target element by id
not href
. 因为你正在使用
#
所以它将通过id
而不是href
定位元素。
Also, because id
is unique, you just need to use: 另外,因为
id
是唯一的,你只需要使用:
$('#upotrebljeni').click(function (e) {
instead of: 代替:
$('a#upotrebljeni').click(function (e) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.