简体   繁体   中英

Using JQuery to create a pop-up when clicking on a tr in a table

I have a table in a jsp and want to create a pop up window when clicking on a table. Included is a jsfiddle with code and an attempt to connect javascript with a specific row but have not had success creating a pop-up.

This fiddle is an example - code I have currently includes a java for loop creating each tr with specific info from a database.

<tr OnClick="display('test');">
      <td><strong>showSpeed</strong></td>
      <td>15</td>
      <td>The speed of the show/reveal</td>
    </tr>

 <script>function display(test) {
        //display a pop up?
    }</script>

https://jsfiddle.net/y2y1w24L/1/

Thank you.

You can use JQuery UI to display a nice popup. This code was made merging your code with the example found here: http://jqueryui.com/dialog/

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <!--<link rel="stylesheet" href="/resources/demos/style.css">-->
</head>
<body>
    <script>
        function display(test) {
            $("#dialog").dialog();
        }
    </script>
    <table>
        <tr onclick="display('test');">
            <td><strong>showSpeed</strong></td>
            <td>15</td>
            <td>The speed of the show/reveal</td>
        </tr>
    </table>

    <div id="dialog" title="Basic dialog" style="display:none">
        <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
    </div>
</body>
</html>

That's one option. You can also take a look at bootstrap modals found here: http://getbootstrap.com/javascript/#modals

Inside your function "display", event.target will give you a reference to whatever the user clicked:

function display(test) {
    alert(event.target.outerHTML);
}

https://jsfiddle.net/y2y1w24L/2/

You should be able to use that to display the appropriate popup.

May this help you

var PopUP=document.createElement("div");
PopUP.className="PopUP";
PopUP.innerHTML="<div id='mainDivBack' style='display:block;'><div id='PopUpDiv' style='display:block;'>"+" Welcome"+ "<input type='button' value='x' id='btnClose' style='display:block;' onclick='popClose();'/></div></div>";
document.body.appendChild(PopUP);
function popClose() {
        document.getElementById("btnClose").style.display = 'none';
        document.getElementById("PopUpDiv").style.display = 'none';
        document.getElementById("mainDivBack").style.display = 'none';

    }
function display(test) {
        PopUP.innerHTML="<div id='mainDivBack' style='display:block;'><div id='PopUpDiv' style='display:block;'>"+test+ "<input type='button' value='x' id='btnClose' style='display:block;' onclick='popClose();'/></div></div>";

        document.getElementById("btnClose").style.display = 'block';
        document.getElementById("PopUpDiv").style.display = 'block';
        document.getElementById("mainDivBack").style.display = 'block';
}

and CSS for this #btnClose { background: none repeat scroll 0 0 #dd2904; border: 1px solid #c13031; color: white; font-size: 18px; font-weight: bold; margin: 5px; padding: 0 1px 4px; position: absolute; right: 10px; top: 0; } #btnClose:hover { background: none repeat scroll 0 0 #ff4565;

    }
    #PopUpDiv
    {
        background: none repeat scroll 0 0 lightgray;
        border: 1px solid gray;
        border-radius: 8px;
        box-shadow: 1px 1px 12px 3px white;
        font-family: "Lucida Console" ,arial;
        height: 70px;
        padding: 35px;
        position: absolute;
        width: 300px;
    }
    #mainDivBack
    {
        background: radial-gradient(lightgray, transparent) repeat scroll 0 0 transparent;
        height: 100%;
        left: 0;
        padding: 20% 25%;
        position: fixed;
        top: 0;
        width: 100%;
    }
    .Grid
    {
        background-color: #fff;
        border: 1px solid #525252;
        border-collapse: collapse;
        color: #474747;
        float: left;
        font-family: Calibri;
        width: 99%;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM