简体   繁体   English

使用jQuery获取元素的HTML标签属性

[英]Getting HTML tag attribute of an element using jQuery

How would I alert the user if they selected more than one COP1000 and in another instance alert the user if they selected more than one COP 2830. Right now it notifies the user if they selected more than one class. 如果他们选择了一个以上的COP1000,我将如何警告用户,而在另一个实例中,如果他们选择了一个以上的COP 2830,我将如何警告用户。现在,如果他们选择了一个以上的类别,它会通知用户。

<script type="text/javascript" charset="utf-8">
var p = 0;
$(document).ready(function () {
    $('input:checkbox').click(function () {
        var COP1000 = $(this).attr('data');
        if ($('input:checkbox:is_checked').attr()) {
            p++;
        }
        if (p >= 2) {
            alert('You can only select this class once');
            return false;
        }
    });
});
</script>

Below are checkboxes with data being the name of the class 以下是复选框,其中数据是类的名称

<input name="class[]" data="COP1000" value="<tr><td>COP1000 <td>8:00AM-9:00AM 
</td><td>MWF </td><td>Sanford </td><td>3</td></tr>" type="checkbox" class="auto-
style88"/>
<input name="class[]" data="COP1000" value="<tr><td>COP1000 <td>11:30AM-12:00PM
</td><td>TTH </td><td>Altamonte </td><td>3</td></tr>" type="checkbox" class="auto-
style88" />

<input name="class[]" data="COP2830" value="<tr><td>COP2830 <td>11:00AM-12:00PM 
</td><td>MWF </td><td>Sanford </td><td>3</td></tr>" type="checkbox" class="auto-
style88"/>
<input name="class[]" data="COP2830" value="<tr><td>COP2830 <td>6:00PM-8:45PM 
</td><td>T </td><td>Altamonte </td><td>3</td></tr>" type="checkbox" class="auto-
style88"/>

This should work: 这应该工作:

$(document).ready(function() {
    $('input:checkbox').click(function() {

        var COP1000Counter = 0;
        var COP2830Counter = 0;


        $('input:checkbox:checked').each(function(){
            if($(this).attr('data') == "COP1000")
            {
                COP1000Counter++;
            } else if ($(this).attr('data') == "COP2830"){
                COP2830Counter++;
            }
        });

        if (COP1000Counter > 1 || COP2830Counter > 1) {
            alert('You can only select this class once');
            return false;
        }
    });
});

By adding more counters or in the if other numbers you can play with all classes and the number of allowed occurences 通过添加更多的计数器或在其他数字中,您可以使用所有类别和允许出现的次数

Fiddle 小提琴

Here you go, check this fiddle out. 在这里,请检查此小提琴

though I feel like my solution is rather crude and can still be improved. 尽管我觉得我的解决方案还很粗糙,仍然可以改进。 but it's a start. 但这是一个开始。

hope it helps. 希望能帮助到你。 :) :)

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

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