Jquery触发器与锚标签不起作用。

huangapple go评论77阅读模式
英文:

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-&gt;session-&gt;flashdata(&#39;img_error&#39;))
{    
    echo &quot;&lt;script language=&#39;javascript&#39;&gt;
        	alert(&#39;its working&#39;);
        	$(&#39;#kyc2&#39;).click();
    	&lt;/script&gt;
    &quot;;

    echo &#39;
        &lt;div class=&quot;alert alert-danger&quot;&gt;
        &#39;.$this-&gt;session-&gt;flashdata(&quot;img_error&quot;).&#39;
        &lt;/div&gt;
    &#39;;
}

Here is html code

&lt;li class=&quot;nav-item&quot;&gt;
	&lt;a  class=&quot;nav-link&quot; id=&quot;kyc2&quot; data-toggle=&quot;tab&quot; href=&quot;#KYC&quot; role=&quot;tab&quot; aria-controls=&quot;KYC&quot;&gt;
		&lt;img src=&quot;&lt;?php echo base_url(); ?&gt;assets/frontend/images/icon/manage.svg&quot; alt=&quot;Image&quot;&gt;    
		&lt;span&gt;KYC&lt;/span&gt;
	&lt;/a&gt;
&lt;/li&gt;

答案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 you click() on it.

Change the PHP part to something like this

if($this-&gt;session-&gt;flashdata(&#39;img_error&#39;))
{    
    echo &quot;&lt;script language=&#39;javascript&#39;&gt;
        	alert(&#39;its working&#39;);
        	$(&#39;#kyc2&#39;).click();
    	&lt;/script&gt;
    &quot;;

    echo &#39;
        &lt;div class=&quot;alert alert-danger&quot;&gt;
        &#39;.$this-&gt;session-&gt;flashdata(&quot;img_error&quot;).&#39;
        &lt;/div&gt;
    &#39;;
}

huangapple
  • 本文由 发表于 2023年2月27日 13:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定