Why can't radio buttons be “readonly”?

前端 未结 13 969
误落风尘
误落风尘 2020-11-27 10:45

I would like to show a radio button, have its value submitted, but depending on the circumstances, have it not editable. Disabled doesn\'t work, because it doesn\'t submit t

相关标签:
13条回答
  • 2020-11-27 11:07

    JavaScript way - this worked for me.

    <script>
    $(document).ready(function() {
       $('#YourTableId').find('*').each(function () { $(this).attr("disabled", true); });
    });
    </script>
    

    Reason:

    1. $('#YourTableId').find('*') -> this returns all the tags.

    2. $('#YourTableId').find('*').each(function () { $(this).attr("disabled", true); }); iterates over all objects captured in this and disable input tags.

    Analysis (Debugging):

    1. form:radiobutton is internally considered as an "input" tag.

    2. Like in the above function(), if you try printing document.write(this.tagName);

    3. Wherever, in tags it finds radio buttons, it returns an input tag.

    So, above code line can be more optimized for radio button tags, by replacing * with input: $('#YourTableId').find('input').each(function () { $(this).attr("disabled", true); });

    0 讨论(0)
  • 2020-11-27 11:08

    I've come up with a javascript-heavy way to achieve a readonly state for check boxes and radio buttons. It is tested against current versions of Firefox, Opera, Safari, Google Chrome, as well as current and previous versions of IE (down to IE7).

    Why not simply use the disabled property you ask? When printing the page, disabled input elements come out in a gray color. The customer for which this was implemented wanted all elements to come out the same color.

    I'm not sure if I'm allowed to post the source code here, as I developed this while working for a company, but I can surely share the concepts.

    With onmousedown events, you can read the selection state before the click action changes it. So you store this information and then restore these states with an onclick event.

    <input id="r1" type="radio" name="group1" value="r1" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);" checked="checked">Option 1</input>
    <input id="r2" type="radio" name="group1" value="r2" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);">Option 2</input>
    <input id="r3" type="radio" name="group1" value="r3" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);">Option 3</input>
    
    <input id="c1" type="checkbox" name="group2" value="c1" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);" checked="checked">Option 1</input>
    <input id="c2" type="checkbox" name="group2" value="c2" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);">Option 2</input>
    <input id="c3" type="checkbox" name="group2" value="c3" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);" checked="checked">Option 3</input>
    

    The javascript portion of this would then work like this (again only the concepts):

    var selectionStore = new Object();  // keep the currently selected items' ids in a store
    
    function storeSelectedRadiosForThisGroup(elementName) {
        // get all the elements for this group
        var radioOrSelectGroup = document.getElementsByName(elementName);
    
        // iterate over the group to find the selected values and store the selected ids in the selectionStore
        // ((radioOrSelectGroup[i].checked == true) tells you that)
        // remember checkbox groups can have multiple checked items, so you you might need an array for the ids
        ...
    }
    
    function setSelectedStateForEachElementOfThisGroup(elementName) {
        // iterate over the group and set the elements checked property to true/false, depending on whether their id is in the selectionStore
        ...
    
        // make sure you return false here
        return false;
    }
    

    You can now enable/disable the radio buttons/checkboxes by changing the onclick and onmousedown properties of the input elements.

    0 讨论(0)
  • 2020-11-27 11:09

    I found that use onclick='this.checked = false;' worked to a certain extent. A radio button that was clicked would not be selected. However, if there was a radio button that was already selected (e.g., a default value), that radio button would become unselected.

    <!-- didn't completely work -->
    <input type="radio" name="r1" id="r1" value="N" checked="checked" onclick='this.checked = false;'>N</input>
    <input type="radio" name="r1" id="r1" value="Y" onclick='this.checked = false;'>Y</input>
    

    For this scenario, leaving the default value alone and disabling the other radio button(s) preserves the already selected radio button and prevents it from being unselected.

    <!-- preserves pre-selected value -->
    <input type="radio" name="r1" id="r1" value="N" checked="checked">N</input>
    <input type="radio" name="r1" id="r1" value="Y" disabled>Y</input>
    

    This solution is not the most elegant way of preventing the default value from being changed, but it will work whether or not javascript is enabled.

    0 讨论(0)
  • 2020-11-27 11:11

    I have a lengthy form (250+ fields) that posts to a db. It is an online employment application. When an admin goes to look at an application that has been filed, the form is populated with data from the db. Input texts and textareas are replaced with the text they submitted but the radios and checkboxes are useful to keep as form elements. Disabling them makes them harder to read. Setting the .checked property to false onclick won't work because they may have been checked by the user filling out the app. Anyhow...

    onclick="return false;"
    

    works like a charm for 'disabling' radios and checkboxes ipso facto.

    0 讨论(0)
  • 2020-11-27 11:11

    The best solution is to set the checked or unchecked state (either from client or server) and to not let the user change it after wards (i.e make it readonly) do the following:

    <input type="radio" name="name" onclick="javascript: return false;" />
    
    0 讨论(0)
  • 2020-11-27 11:11

    What about capturing an "On_click()" event in JS and checking it like here?:

    http://www.dynamicdrive.com/forums/showthread.php?t=33043

    0 讨论(0)
提交回复
热议问题