有时我们会需要在js方法中用到非表单元素的值,但是一般都不知道怎么才能取到??例如我在jsp页面中取出session的值
var password_check_old=document.getElementById("password_check_old");
if(password.value!= e.getAttribute("thid")){
password_check_old.style.display="block";
//form.password.focus();
return false;
}
else
{password_check_old.style.display="none";
}
}
<td><input type="password" id="password" οnblur="check2(this)" thid="<%=name%>" ></td>
</tr>
<tr>
<% String name =(String)request.getAttribute("username");%>
然后需要在表单元素中取到的值和该值做比较,即利用该值在js的function中做一系列判断,这时又无法直接就在js里面用到name的值,那么该怎么做呢?
下面我们可以将该name的值以参数的形式传到js进去
代码示例如下:
js的function方法:
function check2(e){
var password=document.getElementById("password");
var password_check_old=document.getElementById("password_check_old");
if(password.value!= e.getAttribute("thid")){
password_check_old.style.display="block";
//form.password.focus();
return false;
}
else
{password_check_old.style.display="none";
}
}
form表单:
<tr height="25">
<td style="text-align:right">旧密码:</td>
<td><input type="password" id="password" οnblur="check2(this)" thid="<%=name%>" ></td>
</tr>
<tr>
上面红色的两句就是关键的传值和取值的语句,积累起来总会有用的。