Get Default Value From select field Javascript

后端 未结 1 442
广开言路
广开言路 2021-01-19 17:53

I have a select field that have have present options and I have a php script check a mysql database and add a select tag to the one currently being used.

I need to f

相关标签:
1条回答
  • 2021-01-19 18:29

    The Option tag has a javascript attribute defaultSelected that's set to true if the option had a selected value on page load. Note that this is the option tag and not the select one, so you'll have to loop through the options and check for the attribute.

    Something like

    var DAccess;
    var AccessSelect = document.forms[UIDF]["Access"];
    
    for (var i = 0 ; i<AccessSelect.length ; i++)
    {
        if (AccessSelect[i].defaultSelected)
            DAccess = AccessSelect[i].value;
    }
    

    Or, more to the point:

    var AccessHasChanged = false;
    var AccessSelect = document.forms[UIDF]["Access"];
    
    for (var i = 0 ; i<AccessSelect.length ; i++)
    {
        if (AccessSelect[i].defaultSelected && 
            AccessSelect.value != AccessSelect[i].value)
            AccessHasChanged = true;
    }
    
    0 讨论(0)
提交回复
热议问题