英文:
Jquery trigger not working with anchor tag
问题
PHP代码部分:
if($this->session->flashdata('img_error'))
{
echo "<script language='javascript'>
alert('its working');
$('#kyc2').click();
</script>";
echo '
<div class="alert alert-danger">' . $this->session->flashdata("img_error") . '</div>
';
}
HTML代码部分:
<li class="nav-item">
<a class="nav-link" id="kyc2" data-toggle="tab" href="#KYC" role="tab" aria-controls="KYC">
<img src="<?php echo base_url(); ?>assets/frontend/images/icon/manage.svg" alt="Image">
<span>KYC</span>
</a>
</li>
英文:
I am working with jquery and php,Right now i have anchor tag and i want to click/trigger after form submit automatically, For this i am using following code,i am getting alert but button is not "triggered", Where i am wrong ?
PHP Code
if($this->session->flashdata('img_error'))
{
echo "<script language='javascript'>
alert('its working');
$('#kyc2').click();
</script>
";
echo '
<div class="alert alert-danger">
'.$this->session->flashdata("img_error").'
</div>
';
}
Here is html code
<li class="nav-item">
<a class="nav-link" id="kyc2" data-toggle="tab" href="#KYC" role="tab" aria-controls="KYC">
<img src="<?php echo base_url(); ?>assets/frontend/images/icon/manage.svg" alt="Image">
<span>KYC</span>
</a>
</li>
答案1
得分: 1
- PHP将双引号内的
$
视为变量,因此您必须将其转义为原始字符以返回它。 - 在单击之前,您可能还希望等待
#kyc2
加载到DOM中。
将PHP部分更改为以下内容:
if ($this->session->flashdata('img_error')) {
echo "<script language='javascript'>
alert('its working');
$('#kyc2').click();
</script>";
echo '
<div class="alert alert-danger">' . $this->session->flashdata("img_error") . '</div>';
}
英文:
- PHP treats
$
in a double quote as a variable so you have to
escape it to return it as a raw character. - You might also want to wait for
#kyc2
to be loaded into the DOM
before youclick()
on it.
Change the PHP part to something like this
if($this->session->flashdata('img_error'))
{
echo "<script language='javascript'>
alert('its working');
$('#kyc2').click();
</script>
";
echo '
<div class="alert alert-danger">
'.$this->session->flashdata("img_error").'
</div>
';
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论