根据树结构从id中检索值

时间:2015-11-06 12:59:36

标签: javascript jquery html

目标:
当您按下特定按钮例如按钮id =" button_a"时,您应该从id =" a1"中检索该值。然后它应显示为警报消息。

问题:
当您有一个介于100到200行之间的表时,您无法在javascript中对特定的ID名称进行硬编码。

问题:
你应该如何检索例如id =" a1"的值。当你按下按钮?

我已经尝试并了解到你使用了#34; previousSibling" (http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_node_previoussibling)但它比我预期的更复杂。

<table>
<tr>
    <td><div id="a1"><div id="aa1">aa1</div></div></td>
    <td><div id="a2"><div id="aa2">aa2</div></div></td>
    <td><button id="button_a"></button></td>
    <td></td>   
</tr>
<tr>
    <td><div id="b1"><div id="bb1">bb1</div></div></td>
    <td><div id="b2"><div id="bb2">bb2</div></div></td>
    <td><button id="button_b"></button></td>
    <td></td>       
</tr>
<tr>
    <td><div id="c1"><div id="cc1">cc1</div></div></td>
    <td><div id="c2"><div id="cc2">cc2</div></div></td>
    <td><button id="button_c"></button></td>
    <td></td>       
</tr>
<tr>
    <td><div id="d1"><div id="dd1">dd1</div></div></td>
    <td><div id="d2"><div id="dd2">dd2</div></div></td>
    <td><button id="button_d"></button></td>
    <td></td>       
</tr>
</table>

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您需要点击按钮时var push = PushNotification.init({ "android": {"senderID": "12345679"}, "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } ); <!DOCTYPE HTML> <html lang = "en"> <head> <link rel="stylesheet" type="text/css" href="mystylesheet.css"> <title>Tech Order Department.html</title> <meta charset = "UTF-8" /> </head> <body> <!--Designing my form--> <h2>New Projects</h2> <br> <?php if($_POST['formSubmit'] == "Submit") { $varMovie = $_POST['formProject']; $varMovie = $_POST['formClient']; $varMovie = $_POST['formLastName']; $varMovie = $_POST['formDateReceived']; } ?> <form action="myform.php" method="post"> Project:<br> <input type="text" name="Project"> <br> Client:<br> <input type="text" name="Client"> <br> Last Name:<br> <input type="text" name="LastName"> <br> Date Received:<br> <input type="text" name="DateReceived"> <br><br> <input type="submit" name="formSubmit" value="Submit"> </form> <?php if($_POST['formSubmit'] == "Submit") { $errorMessage = ""; if(empty($_POST['formProject'])) { $errorMessage .= "<li>You forgot to enter a project name!</li>"; } if(empty($_POST['formClient'])) { $errorMessage .= "<li>You forgot to enter a client name!</li>"; } if(empty($_POST['formLastName'])) { $errorMessage .= "<li>You forgot to enter the tech writer name!</li>"; } if(empty($_POST['formDateReceived'])) { $errorMessage .= "<li>You forgot to enter the date received!</li>"; } $varMovie = $_POST['formProject']; $varName = $_POST['formClient']; $varMovie = $_POST['formLastName']; $varMovie = $_POST['formDateReceived']; if(!empty($errorMessage)) { echo("<p>There was an error with your form:</p>\n"); echo("<ul>" . $errorMessage . "</ul>\n"); } } ?> </body> </html> 的值。您可以使用:

&#13;
&#13;
div
&#13;
tr
&#13;
&#13;
&#13;

<强>参考

.parents()

.find()

:first-child

答案 1 :(得分:1)

使用jQuery,您可以轻松地按树导航。您可以做到以下几点:

$('#button_a').on('click', function(e) {
    var div_a1 = $(this).closest('tr').find('div').first();
    // and so on
}

jQuery documentation

了解详情