简体   繁体   English

如何在表中查找值,然后在相邻单元格中获取数据

[英]How to find a value in a table, and then grab data in adjacent cells

I have a page that allows users to add rows to a table 1) adding text into 3 input fields, and then 2) clicking on a button. 我有一个页面,允许用户向表添加行1)将文本添加到3个输入字段,然后2)单击按钮。

Before I add a row to the table, I want to make sure that they do not already have an identical record.... 在我向表中添加一行之前,我想确保它们没有相同的记录....

Here's the code I have right now to just add a record: 这是我现在只需添加记录的代码:

    $("#add_to_table").click(function()   {

            var contact_type=$("#contact_types option:selected").val();
            var call_order = $('#call_order').val();
            var contact_details = $('#contact_details').val();

            // add new logic here

            var rules_count = $('#rule_summary tbody tr').length - 1;  //minus one for the
            rules_count = rules_count + 1;

            var htmlstring = '<tr id="rule_' + rules_count + '">'
            htmlstring = htmlstring + '<td><input type="button" value="Remove" class="removeruleset"/></td>'
            htmlstring = htmlstring + '<td>' + contact_type + '</td>'
            htmlstring = htmlstring + '<td id=' + contact_details + '-' + rules_count + '>' + contact_details + '</td>'
            htmlstring = htmlstring + '<td>' + call_order + '</td>'
            htmlstring = htmlstring + '</tr>'
            htmlstring = htmlstring + '<tr><input type="hidden" name="contact_type"' + rules_count + ' value=' + contact_type  + '/></tr>'
            htmlstring = htmlstring + '<tr><input type="hidden" name="contact_details"' + rules_count + ' value=' + contact_details + '/></tr>'
            htmlstring = htmlstring + '<tr><input type="hidden" name="contact_details"' + rules_count + ' value=' + call_order + '/></tr>'
            $('#rule_summary tr:last').after(htmlstring);
    });

This code is working, although I'm sure there are ways to improve it. 这段代码正在运行,虽然我确信有办法改进它。 I'm still pretty green with jQuery and Javascript. 我对jQuery和Javascript仍然非常环保。

Here's what I have come up with so far to change the code: (I'll be inserting this where I have the comment "add new logic here". 这是我到目前为止所提出的改变代码的方法:(我将在这里添加注释“在这里添加新逻辑”。

var cell;
var result = $('#rule_summary tr').find('td:contains('+contact_details+')');

result.siblings().css('background-color', 'red'); result.siblings()。css('background-color','red');

I basically need to check to see if the first cell in the row and the last cell also match the values I've grabbed from the input fields. 我基本上需要检查行中的第一个单元格和最后一个单元格是否也与我从输入字段中获取的值匹配。 I've confirmed that the code is finding the right cell by color coding the adjacent cells. 我已经确认代码是通过对相邻单元格进行颜色编码来找到正确的单元格。 I just need to know how to extract the values for each sibling... one at a time. 我只需要知道如何为每个兄弟提取值...一次一个。

EDIT 1 编辑1

I've also tried to add a class to the fields in the table and select the siblings by class name: 我还尝试在表中的字段中添加一个类,并按类名选择兄弟节点:

var cell;
var result = $('#rule_summary tr').find('td:contains('+contact_details+')');
result.siblings().css('background-color', 'red');

alert(result.siblings('td.call_order').val()); 警报(result.siblings( 'td.call_order')VAL());

But the alert is coming back with nothing. 但警报一无所获。

Is this what you want? 这是你想要的吗?

If $('#rule_summary tr > td:contains('+contact_details+')').length {
    //theres a duplicate entry
}
else {
    //add new entry
}

EDIT 1 To get the text inside each: 编辑1要获取每个文本:

result.siblings().each(function(){
    alert(this.html());
});

EDIT 2 编辑2

To get specific one: 要获得具体的一个:

result.siblings(".yourId").html();

Here's the solution I've come up with: 这是我提出的解决方案:

            var result = $('#rule_summary tr').find('td:contains('+contact_details+')'); //find cell in table with same contact_details information... 

                if (result.length > 0 ) {
                    if (result.siblings("#call_order").html() == call_order && result.siblings("#contact_type").html() == contact_type){
                        alert("Duplicate!");
                        return false;
                    }
                }

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

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