Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript访问表单选择标记的value属性的内容或其内部文本?_Javascript_Jquery_Html_Dom_Xhtml - Fatal编程技术网

如何使用JavaScript访问表单选择标记的value属性的内容或其内部文本?

如何使用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

我是JavaScript新手,在编写表单验证脚本时遇到了一些问题

因此,在我的页面中有一些输入字段,例如:

<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....)