无法在链接上触发单击事件

时间:2021-04-09 21:03:47

My problem is that I got 2 aspx controls generated like this :

我的问题是我生成了2个这样的aspx控件:

<a id="sortByDate" href="javascript:__doPostBack('sortByDate','')">Date</a>
<a id="sortByLastName" href="javascript:__doPostBack('sortByLastName','')">Last name</a>

so those links allow you to sort the results. I'm trying to put this in a combobox instead of using links.

这些链接允许你对结果进行排序。我试着把它放到一个组合框中而不是使用链接。

So I made it like this

我是这样做的

<select id="sortBySelect" onchange="javascript:sortBy(this);">
       <option value="sortByLastName">Last name</option>
       <option value="sortByDate">Date</option>
</select>

with this javascript function

这个javascript函数

function sortBy(sel) {
    var id = sel.value;
    $("#" + id).trigger("click");
}

So when you change the selected element in the combobox I want to trigger the click event on the link to call the dopostback to sort.

因此,当您更改组合框中选定的元素时,我希望触发链接上的单击事件,以调用dopostback进行排序。

It does nothing so far. I tried "click", "onclick", "onClick" and nothing works. Unfortunately, this is for IE quirks mode.

到目前为止它什么也没做。我尝试了“点击”、“onclick”、“onclick”,但什么都不管用。不幸的是,这是IE的模式。

I know, this is really not elegant, but I'm really short in time and I need something quick and dirty. I will make an aspx control eventually to handle this nicely.

我知道,这真的不优雅,但我的时间真的很短,我需要一些快速和肮脏的东西。我将最终制作一个aspx控件来很好地处理这个问题。

Any ideas how I could make this work in ie quirks mode?

有什么办法能让我在ie的模式下工作吗?

Thank you

谢谢你!

4 个解决方案

#1


4  

Try changing the location of the page:

尝试改变页面的位置:

document.location = $("#" + id).attr('href');

#2


2  

why don't you just fire the __doPostBack directly:

你为什么不直接开火?

function sortBy(sel) {
    var id = sel.value;
    __doPostBack(id,'');   
}

#3


1  

Use:

使用:

function sortBy(sel) { var id = sel.value; alert($("#" + id).length);//just for conforming that the element exists. $("#" + id).click(); }

函数sortBy(sel) {var id = sel.value;警告($(“#”+ id).length);//只是为了符合元素的存在。$(" # " + id).click();}

Actually, jQuery only triggers any event bound to an element. You are trying to call the href action.

实际上,jQuery只触发绑定到元素的任何事件。您正在尝试调用href动作。

If you don't want to change the markup, you could try this solution.

如果您不想更改标记,可以尝试此解决方案。

Otherwise, you will have to change your html markup to bind javascript events & then try triggering it.

否则,您将不得不更改html标记来绑定javascript事件,然后尝试触发它。

Good luck!

好运!

#4


1  

Everything works as intended!

一切按预期的方式工作!

JSFiddle proof.

Make sure you have an element with the id sortById and sortByLastName !

确保您有一个带有id sortById和sortByLastName的元素!


JavaScript/jQuery

$(document).ready(function(){
    $('#sortByDate').click(function(){
        alert('Sort By Date Click Event fired!');
    })
});
function sortBy(sel) {
    var id = sel.value;
    $("#" + id).trigger("click");
}

Alternative

Force the window.location change

力的窗口。位置变化

JavaScript/jQuery

function sortBy(sel) {
    var id = sel.value;
   window.location =  $("#" + id).attr('href');
}

Note: You won't see any change in JSFiddle:

注意:JSFiddle没有变化:

Refused to display 'https://www.google.ca/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. 

#1


4  

Try changing the location of the page:

尝试改变页面的位置:

document.location = $("#" + id).attr('href');

#2


2  

why don't you just fire the __doPostBack directly:

你为什么不直接开火?

function sortBy(sel) {
    var id = sel.value;
    __doPostBack(id,'');   
}

#3


1  

Use:

使用:

function sortBy(sel) { var id = sel.value; alert($("#" + id).length);//just for conforming that the element exists. $("#" + id).click(); }

函数sortBy(sel) {var id = sel.value;警告($(“#”+ id).length);//只是为了符合元素的存在。$(" # " + id).click();}

Actually, jQuery only triggers any event bound to an element. You are trying to call the href action.

实际上,jQuery只触发绑定到元素的任何事件。您正在尝试调用href动作。

If you don't want to change the markup, you could try this solution.

如果您不想更改标记,可以尝试此解决方案。

Otherwise, you will have to change your html markup to bind javascript events & then try triggering it.

否则,您将不得不更改html标记来绑定javascript事件,然后尝试触发它。

Good luck!

好运!

#4


1  

Everything works as intended!

一切按预期的方式工作!

JSFiddle proof.

Make sure you have an element with the id sortById and sortByLastName !

确保您有一个带有id sortById和sortByLastName的元素!


JavaScript/jQuery

$(document).ready(function(){
    $('#sortByDate').click(function(){
        alert('Sort By Date Click Event fired!');
    })
});
function sortBy(sel) {
    var id = sel.value;
    $("#" + id).trigger("click");
}

Alternative

Force the window.location change

力的窗口。位置变化

JavaScript/jQuery

function sortBy(sel) {
    var id = sel.value;
   window.location =  $("#" + id).attr('href');
}

Note: You won't see any change in JSFiddle:

注意:JSFiddle没有变化:

Refused to display 'https://www.google.ca/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.