在动态创建的RadioButton选择中显示Div(PHP-AJAX)

时间:2016-10-04 04:15:27

标签: javascript php radiobuttonlist

我创建了单选按钮是在客户端创建的,当我点击它时,它应该显示我现有的div,即ifPrint

请查看我正在尝试的代码:

单选按钮创建:

var optiondiv = document.getElementById('option-div');

document.getElementById('create').onclick = function () {

newopt = document.getElementById('new-option').value;
if(newopt){
    var input = document.createElement('input'),
    label = document.createElement('label');
    input.type = "radio";
    input.setAttribute("value", newopt);
    input.setAttribute("checked", true);
    input.setAttribute("name", "radio-name");
    label.appendChild(input);
    label.innerHTML += newopt+'<br>'; 
    optiondiv.appendChild(label);
    document.getElementById('new-option').value = '';

    $.post(
        "equipments1.php", 
        { 
            "newopt": newopt  
        }, 
        function(data) {
            if(data.success){
                alert('Successful Added To dB');
                document.getElementById('newopt').checked = false;

            //  if (document.getElementById('newopt').checked) {
//document.getElementById('ifPrint').style.display = 'block';
        //$(#ifPrint).show();
                //$(#ifPrint).show();
            }else{
                alert('Not Add To DB');

            }

    });

}else{
    alert('Please Enter Radio Button Value.');
    return false;
}



};

显示Div ifPrint的新功能:

$('input[type=radio][name=newopt]').change(function() {
$(#ifPrint).show();

});

请尽可能指导

1 个答案:

答案 0 :(得分:1)

你需要使用点击而不是更改,而且你的代码中也缺少引号,如果在dinamicaly之后创建元素,最好使用“on”而不是“click”函数

$('body').on('click','input[type=radio][name=newopt]',function () {
    $('#ifPrint').show();
});
相关问题