获取attr ID并在jQuery中使用IF / ELSE

时间:2013-11-16 18:23:38

标签: javascript jquery

基本上我想获取所选单选按钮的ID并在IF / ELSE语句中使用ID以分配某个变量。这就是我所拥有的:

   $(document).ready(function() {

    $('input[type=radio]').click(function() {
        var id = $("#radio :radio:checked").attr('id');
        if(id == 'radio-bio') { 
        $('#feedContainer').empty();
                    $.ajax({
                    type: 'GET',
                    url: 'http://www.e-grid.net/BayAreaTech/wp-rss2.php?cat=15',
                    dataType: 'xml',
                    success: function (xml) {
                        $(xml).find("item").each(function () {
                              var title = $(this).find("title").text();
                              var date = $(this).find("Date").text();
                              var region = date.substr(6);
                                    if (region.length < 3) { region = "ALL"; }  
                              var description = $(this).find("description").text();
                              var descriptdisplay = description.substr(0, description.indexOf(",")+6); //Parsed DATE from description
                                    if (descriptdisplay.length > 35) { descriptdisplay = "See event for details"; }
                                //var locationdisplay = description.substr(description.indexOf(",")+6,4); //Parsed the location from description
                              var category = $(this).find("category").text();
                              var linkUrl = $(this).find("link").text();
                              var displaytitle = "<a href='" + linkUrl + "' target='_blank'>" + title + "</a>"  
                              $('#feedContainer').append('<h3>'+displaytitle+'</h3><p>'+"Event Date: "+descriptdisplay+'</p><p>'+"Location: "+region+'</p');

单选按钮信息:

           

    <input type="radio" name="radio" id="radio-ALL">
    <label for="radio-ALL">All Events</label>
    <input type="radio" name="radio" id="radio-bio">
    <label for="radio-bio">Bioengineering</label>
    <input type="radio" name="radio" id="radio-com">
    <label for="radio-com">Communications</label>
    <input type="radio" name="radio" id="radio-eleP">
    <label for="radio-eleP">Electrical/Power</label>
     <input type="radio" name="radio" id="radio-eleD">
    <label for="radio-eleD">Electronics/Design</label>
     <input type="radio" name="radio" id="radio-nano">
    <label for="radio-nano">NanoEngineering</label>
     <input type="radio" name="radio" id="radio-opt">
    <label for="radio-opt">Optics/Display</label>
     <input type="radio" name="radio" id="radio-semi">
    <label for="radio-semi">Semiconductors</label>
</fieldset>

    </ul>

1 个答案:

答案 0 :(得分:1)

只需使用this来引用点击处理程序方法中的被点击元素

var id = this.id

$('input[type=radio]').click(function() {
    var id = this.id;
    if(id == 'radio-bio') {