如何使用JavaScript访问表单选择标记的value属性的内容或其内部文本?
我是JavaScript新手,在编写表单验证脚本时遇到了一些问题 因此,在我的页面中有一些输入字段,例如:如何使用JavaScript访问表单选择标记的value属性的内容或其内部文本?,javascript,jquery,html,dom,xhtml,Javascript,Jquery,Html,Dom,Xhtml,我是JavaScript新手,在编写表单验证脚本时遇到了一些问题 因此,在我的页面中有一些输入字段,例如: <input id="kmProjectInfo_name" class="" type="text" value="" size="19" name="kmProjectInfo.name"> 好的,这项工作很好,但在我的表格中,我还有一个需要验证的字段: <select id="selectStatus" onchange="checkStatus(this)" n
<input id="kmProjectInfo_name" class="" type="text" value="" size="19" name="kmProjectInfo.name">
好的,这项工作很好,但在我的表格中,我还有一个需要验证的字段:
<select id="selectStatus" onchange="checkStatus(this)" name="kmProjectInfo.status.idProjectInfoStatus">
<option value="0">-- Please Select --</option>
<option id="aui_3_2_0_1240" value="1">Closed</option>
<option id="aui_3_2_0_1226" value="2">Active</option>
<option value="3">Testing</option>
<option value="4">Starting</option>
</select>
--请选择--
关闭
忙碌的
测试
启动
因此,现在我需要访问value属性中的值(例如0,1,2,3,4)或选项标记的内部文本(例如:“--请选择--”,“关闭”,“活动”,“测试”,“开始”)
我可以用JavaScript做这件事吗?我如何实现它
Tnx纯Javascript 这允许访问
innerHtml
属性及其内容
var myInnerHtml=document.getElementById(“forloop”).innerHTML代码>
使用JQuery
或者代替.innerHTML
是,您可以获取select
元素的选定索引,然后基于该索引获取选项
值:
var select = document.getElementById("kmProjectInfo_name");
var index = select.selectedIndex;
var value = select.options[index].value; //0, 1, 2, 3, 4
试试这个
var el = document.getElementById("selectStatus");
var value = el.options[el.selectedIndex].value; // get value (1, 2, 3, ...)
var text = el.options[el.selectedIndex].text; // get text (Closed, Starting....)
访问select的innerHTML
如何帮助验证它?糟糕,我太快了,没有把答案仅仅建立在标题和标签上。innerHTML有效,innerText无效。为什么?具体区别是什么?innerText
不是有效的属性。
var el = document.getElementById("selectStatus");
var value = el.options[el.selectedIndex].value; // get value (1, 2, 3, ...)
var text = el.options[el.selectedIndex].text; // get text (Closed, Starting....)