问题描述:

Have a look at following code

function show(id){

if (document.getElementById(id).style.display == 'none')

{

document.getElementById(id).style.display = '';

document.getElementById(id).removeAttribute("disabled");

}

}

function hide(id){

document.getElementById(id).style.display = 'none';

document.getElementById(id).setAttribute("disabled","disabled");

}

http://jsfiddle.net/3jD3W/

When I hide element, i also want to disable it so that value submitted is only for the element that visible now.

However it is not working as expected. What I am doing wrong?

网友答案:

try to set the disabled property

document.getElementById(id).disabled = true; //false
网友答案:
document.getElementById(id).disabled=true;
网友答案:

I don't see you calling hide(id) anywhere in the jsFiddle, perhaps you didn't post the full code. Element.setAttribute('disabled', true) and Element.disabled = true both work. Since you're using jQuery, use prop instead.

$('#desire').change(function() {
    var id = $(this).val(); //returns Share if Share is selected
    var $select = $('#' + id);
    var $actions = $('select[name=action]');
    $actions.hide(); //hide all actions but the selected one
    $select.show().prop('disabled', false); //enable this select
    $('select[name=action]').not($select).prop('disabled', true); //disable all other actions
});

$('input#submit').click(function() {
    $('select[name=action]:hidden').prop('disabled', true); //required in case the form is submitted directly
    alert($('form').serialize()); //only added for testing
    return false; //test
});

jsFiddle Demo

相关阅读:
Top