html的标签,如何获取当前选中的项的内容呢?

如题所述

    html中获取标签的数据,可以通过js来获取,如下代码:

<script type="text/javascript" src="js/jquery.js"></script>

<script type="text/javascript">

function validate(){

//一 .是通过jDOM
// var attitudeObj = document.getElementById("attitude");
//var attitude = attitudeObj.innerHTML;  

//二 .是通过jquery
var att = $("#attitude").text();//可以得到输入域中的内容
var idnum = $("#idnum").val();   


var name = $("#name").val();
var t_name =  $("#t_name").val() ;
var pro_name =  $("#pro_name").val() ;


// alert(att);
if( name == null || name == "" ){

alert("请输入毕业设计名称!");
return false;
}
att   =   att.replace(/\s+/g,"");
if( att == null || att == ""){

alert("请输入选题须知!");
return false;
}
if( t_name == null || t_name == "" ){
alert("请选择教师!");
return false;
}
if( pro_name == null || pro_name == ""){
alert("请选择专业!");
return false;
}

location="teacher/mainfra.jsp";
//location="teacherAction_AddGraduationTitle.action?idnum=" + idnum +"&cname=" + cname +"&start=" + start+"&end=" + end;
return true;
}
</script>
</head>

<body">
<div >
<div style="margin-left: 100px;">
<!--   <form action="teacherAction_AddGraduationTitle.action" method="post" >-->
<span>自动编号:</span><input type="text" readonly="readonly" name="idnum" id="idnum" style="background: gray;" value="<s:property value="#request.maxIdnum"/>"/><br/>
<br/>
<span>毕业设计名称:</span><input type="text" name="name" id="name"/><br/><br/>
<span>所属教师:</span><select name="t_name" id="t_name">
<option></option>
<s:iterator value="teacherList">
<option value="<s:property value='idnum' />"><s:property value="name" /></option>
</s:iterator>
</select> <br/> <br/>
<span>所属专业:</span><select name="pro_name" id="pro_name">
<option></option>
<s:iterator value="professionList">
<option value="<s:property value='idnum' />"><s:property value="pro_name" /></option>
</s:iterator>
</select>
<br/>
<br/>
<span>选题须知:</span>
<textarea id ="attitude" name="attitude" rows="3" cols="30" style="color: red;">
</textarea>
<br/>

<br/>

<input type="button" value="添加">  <input style="margin-left: 100px" type="reset" value="重置">

<!-- </form> -->
</div>
</div>
</body>


温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-11-25
分别使用javascript原生的方法和jquery方法
<select id="test" name="">
<option value="1">text1</option>
<option value="2">text2</option>
</select>
code:
一:javascript原生的方法
1:拿到select对象: var myselect=document.getElementById("test");
2:拿到选中项的索引:var index=myselect.selectedIndex ; // selectedIndex代表的是你所选中项的index
3:拿到选中项options的value: myselect.options[index].value;
4:拿到选中项options的text: myselect.options[index].text;
二:jquery方法(前提是已经加载了jquery库)
1:var options=$("#test option:selected"); //获取选中的项
2:alert(options.val()); //拿到选中项的值
3:alert(options.text()); //拿到选中项的文本
第2个回答  2021-12-02
这个,需要编写JavaScript脚本才能实现。
document.getElementById('content').innerHTML、
document.getElementById('content').innerText、
document.getElementById('content').textContent
这三种方法都可以获取到id为content的标签里面的文本内容
第3个回答  2021-11-24
使用html的<select>标签
相似回答