Check a radio button with javascript

前端 未结 6 1644
一个人的身影
一个人的身影 2020-11-27 04:22

For some reason, I can\'t seem to figure this out.

I have some radio buttons in my html which toggles categories:



        
相关标签:
6条回答
  • 2020-11-27 04:27

    Do not mix CSS/JQuery syntax (# for identifier) with native JS.

    Native JS solution:

    document.getElementById("_1234").checked = true;

    JQuery solution:

    $("#_1234").prop("checked", true);

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

    I was able to select (check) a radio input button by using this Javascript code in Firefox 72, within a Web Extension option page to LOAD the value:

    var reloadItem = browser.storage.sync.get('reload_mode');
    reloadItem.then((response) => {
        if (response["reload_mode"] == "Periodic") {
            document.querySelector('input[name=reload_mode][value="Periodic"]').click();
        } else if (response["reload_mode"] == "Page Bottom") {
            document.querySelector('input[name=reload_mode][value="Page Bottom"]').click();
        } else {
            document.querySelector('input[name=reload_mode][value="Both"]').click();
        }
    });
    

    Where the associated code to SAVE the value was:

    reload_mode: document.querySelector('input[name=reload_mode]:checked').value
    

    Given HTML like the following:

        <input type="radio" id="periodic" name="reload_mode" value="Periodic">
        <label for="periodic">Periodic</label><br>
        <input type="radio" id="bottom" name="reload_mode" value="Page Bottom">
        <label for="bottom">Page Bottom</label><br>
        <input type="radio" id="both" name="reload_mode" value="Both">
        <label for="both">Both</label></br></br>
    
    0 讨论(0)
  • 2020-11-27 04:32

    Easiest way would probably be with jQuery, as follows:

    $(document).ready(function(){
      $("#_1234").attr("checked","checked");
    })
    

    This adds a new attribute "checked" (which in HTML does not need a value). Just remember to include the jQuery library:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    
    0 讨论(0)
  • 2020-11-27 04:41

    By using document.getElementById() function you don't have to pass # before element's id.

    Code:

    document.getElementById('_1234').checked = true;
    

    Demo: JSFiddle

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

    If you want to set the "1234" button, you need to use its "id":

    document.getElementById("_1234").checked = true;
    

    When you're using the browser API ("getElementById"), you don't use selector syntax; you just pass the actual "id" value you're looking for. You use selector syntax with jQuery or .querySelector() and .querySelectorAll().

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

    Today, in the year 2016, it is save to use document.querySelector without knowing the ID (especially if you have more than 2 radio buttons):

    document.querySelector("input[name=main-categories]:checked").value
    
    0 讨论(0)
提交回复
热议问题