如何访问表单 select 标记的值属性的内容或使用 JavaScript 访问其内部文本?
How can I acces to the content of the value attribute of a form select tag or to its inner text using JavaScript?
我绝对是 JavaScript 的新手,我在处理表单验证脚本时遇到了一些问题。
所以在我的页面中有一些输入字段,例如:
<input id="kmProjectInfo_name" class="" type="text" value="" size="19" name="kmProjectInfo.name">
并且我使用以下函数使用 document.getElementById('kmProjectInfo_name').value 从该输入字段中获取值并检查是否为该值是
对我的目的相当有效:
function validateForm() {
alert(document.getElementById('selectCountry').value)
// VALIDAZIONE DEL PROJECT NAME:
if( document.getElementById('kmProjectInfo_name').value == "" )
{
alert( "Please provide a valid project name" );
//document.myForm.Name.focus();
document.getElementById('kmProjectInfo_name').focus();
return false;
}
好的,这个工作正常,但在我的表单中我还有这个需要验证的字段:
<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)或选项标签的内部文本(例如:“--请Select--”,"Closed","Active","Testing","Starting")。
我可以使用 JavaScript 来做这件事吗?我该如何实施?
Tnx
纯Javascript
这允许访问 innerHtml
属性及其内容。
var myInnerHtml = document.getElementById("forloop").innerHTML;
使用JQuery
是的,可以得到select
元素的selected index,然后据此得到option
值:
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....)
我绝对是 JavaScript 的新手,我在处理表单验证脚本时遇到了一些问题。
所以在我的页面中有一些输入字段,例如:
<input id="kmProjectInfo_name" class="" type="text" value="" size="19" name="kmProjectInfo.name">
并且我使用以下函数使用 document.getElementById('kmProjectInfo_name').value 从该输入字段中获取值并检查是否为该值是 对我的目的相当有效:
function validateForm() {
alert(document.getElementById('selectCountry').value)
// VALIDAZIONE DEL PROJECT NAME:
if( document.getElementById('kmProjectInfo_name').value == "" )
{
alert( "Please provide a valid project name" );
//document.myForm.Name.focus();
document.getElementById('kmProjectInfo_name').focus();
return false;
}
好的,这个工作正常,但在我的表单中我还有这个需要验证的字段:
<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)或选项标签的内部文本(例如:“--请Select--”,"Closed","Active","Testing","Starting")。
我可以使用 JavaScript 来做这件事吗?我该如何实施?
Tnx
纯Javascript
这允许访问 innerHtml
属性及其内容。
var myInnerHtml = document.getElementById("forloop").innerHTML;
使用JQuery
是的,可以得到select
元素的selected index,然后据此得到option
值:
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....)