常用JavaScript操作页面元素的方法

时间:2023-03-10 00:05:49
常用JavaScript操作页面元素的方法

1、取得dropdownlist的选中值

 var ddl =document.getElementById('<%=ddlusers.ClientID%>');
var index = ddl.selectedIndex;
var Value = ddl.options[index].value;
var Text = ddl.options[index].text;

2、给textbox文本框赋值

  document.getElementById('<%=txtzfry.ClientID%>').value = str2;

3、循环取得被选中的chexbox的值

  var aa = document.getElementsByName("box");
var v = "";
var title = "";
for (var i = 0; i < aa.length; i++) {
if (aa[i].checked) {
v += aa[i].value + ",";
title += aa[i].title + ","; }
}

4、div的显示隐藏

 function qiehuan(v) {
if (v == "1") {
$("#btn1").show();
$("#btn2").hide(); } else {
$("#btn2").show();
$("#btn1").hide(); }
}

相应的div或者span

 <span id="btn1">
<input name="btnReturn" type="button" value="打印表单" class="btn yellow" onclick="javascript:prn1_preview()" />
</span>
<span id="btn2" style="display:none;">
<input name="btnReturn2" type="button" value="打印表单" class="btn yellow" onclick="javascript:prn2_preview()" />
</span>

5、去掉字符串最后的字符或者逗号,

title.substring(0, title.length - 1);