click event on a link doesn't work in jquery

2019-09-01 06:51发布

if element has class user i need to click on a link. the alert executes but link don't work.

function gotoproduct()
{
    var id = $(this).attr('id');
    if($(this).hasClass("user"))
    {
        alert(""); // alert works.
    $('#mainnavi li a[title = "users"]').click(); // it's not working
    }
    else
    {
    window.location.replace("../"+tab+"/product.php?subjectid="+id+"");
    }
}

html:

<div id="mainnavi" title="">
<ul style="margin-top:10px;">
<li><a title="phones" href="../phones/firstpage.php" >گوشی</a></li>
<li><a title="users" href="../users/firstpage.php" >گوشی</a></li>
</ul>
</div>

1条回答
Rolldiameter
2楼-- · 2019-09-01 07:18

You can try this:

 $('#mainnavi li a[title = "users"]').get(0).click();

Demo: http://jsfiddle.net/a6NJk/652/
try to see request by debugging. it is redirecting :)

there is another alternative way you can do this

like this:

if($(this).hasClass("user"))
    {
     alert(""); // alert works.
      window.location.href = $('#mainnavi li a[title = "users"]').attr("href"); 
    }

sample demo : http://jsfiddle.net/a6NJk/651/

 function doclicking(){
         var link = $('#mainnavi li a[title = "users"]').attr("href");
         window.location.href= link;       
     } 
     doclicking();
查看更多
登录 后发表回答