Jquery动态表验证

时间:2011-02-03 09:48:10

标签: javascript jquery

我有动态表格如图所示。

Image of the table

如何使用jquery或javascript

限制两列中的行具有相同的值

提前致谢

1 个答案:

答案 0 :(得分:2)

更新:Haim在评论中指出,我可能误解了你的想法。所以我会提出两个选择。

如果您想阻止选择相同的值:

$('table').delegate('select', 'change', function () {
    var other = $(this).closest('tr').find('select').not(this);
    if (other.val() == $(this).val()) {
        // Bad! Now what do you want to do?

        // Select nothing?
        this.selectedIndex = -1;
        // Make the row red?
        $(this).closest('tr').css('background-color', 'red');
        // Be reallllly annoying?
        alert('You have made a bad choice!');
    }
});

如果您想保持两列相同:

$('table').delegate('select', 'change', function () {
    $(this).closest('tr').find('select').not(this).val($(this).val());
});

请参阅演示:http://jsfiddle.net/yGSNN/