从选择字段Javascript获取默认值 [英] Get Default Value From select field Javascript
问题描述
我有一个具有当前选项的选择字段,我有一个php脚本检查mysql数据库,并将select标签添加到当前使用的标签中.
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 find a way to know if the user have changed the select field or not before submitting it.
var Access = document.forms[UIDF]["Access"].value;
var DAccess = document.forms[UIDF]["Access"].defaultIndex; <--- This is my problem. I just need to know how to get the selected option value as this keep changing with every user
<select name="Access">
<option value="0" selected='selected'>Employee</option>
<option value="1">Team Leader</option>
<option value="2">Admin</option>
</select>
推荐答案
Option
标记具有javascript属性defaultSelected
,如果该选项在页面加载时具有selected
值,则该属性会设置为true
.请注意,这是option
标记,而不是select
标记,因此您必须遍历选项并检查属性.
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.
类似
var DAccess;
var AccessSelect = document.forms[UIDF]["Access"];
for (var i = 0 ; i<AccessSelect.length ; i++)
{
if (AccessSelect[i].defaultSelected)
DAccess = AccessSelect[i].value;
}
或更重要的是:
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;
}
这篇关于从选择字段Javascript获取默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!