简体   繁体   English

点击行的jQuery切换元素

[英]Jquery toggle element of clicked row

This is supposed to be a client list, where you click the rows to select them and then submit them to database for cron job to pick them up and send email to them. 这应该是一个客户列表,您可以在其中单击行以选择它们,然后将其提交给数据库以进行cron作业以将其提取并发送电子邮件。

I'm trying highlight rows instead of using checkboxes, thats why I put two hidden elements one for the client id, other - bool for checking if row has been clicked or not. 我正在尝试突出显示行而不是使用复选框,这就是为什么我放置两个隐藏元素,一个用于客户端ID,另一个用于布尔值,用于检查是否单击了行。

What I can't figure out is how to toggle the specific row's element value(checked). 我不知道的是如何切换特定行的元素值(选中)。

<table>
<tbody>
<tr class="contactrow">
<input type="hidden" name="id" value="995"/>
<input type="hidden" name="checked" value="FALSE"/>
<td>Andrew Ostrich</td>
<td>AO Company</td>
<td>7537535</td>
<td>ao@aocompany.com</td>
</tr>
<tr class="contactrow">
<input type="hidden" name="id" value="296"/>
<input type="hidden" name="checked" value="FALSE"/>
<td>Brendon Evans</td>
<td>Institute of Modern Arts</td>
<td>648648468</td>
<td>bevans@ioma.co.uk</td>
</tr>
<tr class="contactrow">
<input type="hidden" name="id" value="421"/>
<input type="hidden" name="checked" value="FALSE"/>
<td>Rudy Thompson</td>
<td>Marine CORP</td>
<td>95939288</td>
<td>Rudee@mcorp.es</td>
</tr>
</tbody>
</table>

​ Also, http://jsfiddle.net/FHjAc/ https://jsfiddle.net/FHjAc/

Part of the problem is that the server won't be able to identify which client ids has been checked. 问题的部分原因是服务器将无法识别已检查的客户端ID。 All of the input[type=checkbox] share the same name attribute, which is what is used to identify each parameter. 所有input[type=checkbox]共享相同的name属性,该属性用于标识每个参数。

If you change the name attribute to checked[__ID__] , then you can loop over the parameters and submit those records to the database. 如果将name属性更改为checked[__ID__] ,则可以遍历参数并将这些记录提交到数据库。

In PHP you would do the following. 在PHP中,您可以执行以下操作。

<?php

$checked = $_POST['checked'];

foreach( $checked as $id ) {
  // do something with the $id here
}

Update 更新

I didn't realize you were using a hidden input for the checked field. 我没有意识到您正在为检查字段使用隐藏的输入。 If you change it to a checkbox you're job should be a lot easier. 如果将其更改为复选框,那么您的工作应该会容易得多。

HTML This is how I would markup my HTML, you can use CSS to hide the checkbox with display: none . HTML这就是我要标记HTML的方式,您可以使用CSS隐藏带有display: none的复选框。

<tr class="contactrow">
  <td>
    <input type="checkbox" name="checked[421]" />
    Rudy Thompson
  </td>
  <td>Marine CORP</td>
  <td>95939288</td>
  <td>Rudee@mcorp.es</td>
</tr>

jQuery jQuery的

$(function () {
  $('.contactrow').click(function () {
    var checkbox = $(this).find('input[type=checkbox]');
    checkbox.prop('checked', !checkbox.prop('checked'));
    $(this).toggleClass('contactrow_highlight');
  });  
});

I tested this and it works for me. 我对此进行了测试,它对我有用。

$(function () {
    $('.contactrow').click(function () {
        $(this).toggleClass("contactrow_highlight");
        $(this).find("input").each(function(){
            if($(this).attr("name") == "checked"){
                if($(this).val() == "FALSE"){
                    $(this).val("TRUE");
                } else {
                    $(this).val("FALSE");                   
                }
            }
        });
    });
})

Here you go: http://jsfiddle.net/AuYZf/1/ 您在这里: http : //jsfiddle.net/AuYZf/1/

You can get the siblings of the first child of the row element you clicked, the third appears to be the desired element. 您可以获得单击的行元素的第一个孩子的兄弟姐妹,第三个孩子似乎是所需的元素。 Then you can simply toggle the value: 然后,您可以简单地切换值:

var inputElement = this.firstChild.nextSibling.nextSibling.nextSibling;
inputElement.value = (inputElement.value != "true")

(I used != because the string "false" still evaluates as true) (我使用!=,因为字符串“ false”仍然计算为true)
And it doesn't even use jQuery to loop through everything. 而且它甚至不使用jQuery遍历所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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