JQuery: how to call td a when the tr button is pressed

I have a table where the first td in trs contains a link. I want this anchor to be launched (clicked) no matter where I click in the containing tr.

I have read and tried many posts and suggestions on this topic, but I cannot get this to work. I tried trigger () and triggerHandle () functions, but it doesn't trigger the snap click that I want.

There must be others who needed to trigger an anchor click when clicking the tr button, so that the user does not need to click the tds anchor link. Is this, of course, a good user interface feature, if possible?

Here is the code I tried:

<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />  
    <script type="text/javascript" src="javascripts/jquery-1.4.2.js"></script>
    <script type="text/javascript" charset="utf-8">
        /* Initialise the table with the required column sorting data types */
        jQuery(document).ready(function () {
            jQuery("#rowClick tr").click(function (e) {
                jQuery("#clickevent", this).trigger("click");
            });
        });
    </script> 
</head> 
<body id="dt_example"> 
    <table id="rowClick">
        <thead>
            <tr>
                <th style="width: 30px">id</th>
                <th style="width: 200px">navn</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><a href="datatabletext.asp?test=1" id="clickevent">1</a></td>
                <td>Jesper</td>
            </tr>
            <tr>
                <td><a href="datatabletext.asp?test=2" id="clickevent">2</a></td>
                <td>Bjarne</td>
            </tr>
            <tr>
                <td><a href="datatabletext.asp?test=3" id="clickevent">3</a></td>
                <td>Søren</td>
            </tr>                            
        </tbody>
    </table>
</body> 
+3
source share
5 answers

naugtur Alessandro; tds, "clickevent":

$(document).ready(function () {
    $("#rowClick tr").click(function (e) {
        document.location.href = $(this).find(".clickevent").attr('href');
    });
});
+3

- .

:

document.location.href=$aintd.attr('href');

$aintd - ,

+2
        jQuery("#rowClick tr").click(function (e) {
            jQuery(this).find('td:first a').click();
        });

I must add that naugtur is true that this will not lead you to another page, but everything that happens on the page itself will work fine.

+1
source

Yes, x1a4 is right. And remove the "clickevent" identifiers from your tds; the id attribute must be the "primary key" for the DOM element.

0
source

try the following

 $("#rowClick tr").click(function (e) {
                $(this).find('td:first a').trigger('click');
            });
0
source

Source: https://habr.com/ru/post/1745414/


All Articles