如果我的单选按钮是动态的,如何获取已选中单选按钮的值?

时间:2016-06-08 09:49:25

标签: javascript jquery html

我试图检索用户选择的动态单选按钮的值,但我似乎无法做到这一点。这是代码:

Javascript for dynamic radio buttions:

function setupVisibilityElements() {
    jQuery.ajax({
        url: '/Api/Categories/GetVisibilityData',
        method: 'GET',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        cache: false,
    }).done(function(data, textStatus, jqXHR) {
        var visibilityList = data;
        var visibilityID = '';
        var visibilityName = '';
        var $radioElement = null;
        var oneVisbilityType = null;

        for (index = 0; index < visibilityList.length; index++) {
            oneVisbilityType = visibilityList[index];
            visibilityID = oneVisbilityType.visibilityId;
            visibilityName = oneVisbilityType.visibilityName;
            $radioElement = $('<input type="radio" value = "' + visibilityID + '" name="visibility" />');            
            $visibilityInputElement.append($radioElement);
            $radioElement = $('<span> ' + visibilityName + ' </br></span>');              
            $visibilityInputElement.append($radioElement);
        }
    })
}

用于收集信息的Javascript:

var radioButtonChecker = document.getElementsByName('visibility');

for (var i = 0, length = radioButtonChecker.length; i < length; i++) {
    if (radioButtonChecker[i].checked) {
        console.dir(radioButtonChecker[i].value);
        break;
    }
}

编辑: 我试图检索已选中按钮的值,但它没有显示。没有错误。 http://i.imgur.com/pTJY47X.jpg

0 个答案:

没有答案