javascript 使用 struts html 标签时,如何使用文本框的属性属性获取文本框的值?有没有其他方法可以获得价值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17742601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:26:51  来源:igfitidea点击:

While using struts html tags, How do I get the value of a textbox using its property attribute? Is there any other way to get the value?

javajavascriptjspstruts

提问by Rajaah

    function validate(){
    var username = document.getElementByProperty("userName").value;
    var password = document.getElementByProperty("passWord").value;
    if((username == null ||username =='') || (password == null||password=='')){
     x=document.getElementById("error");
     x.innerHTML = "Username or Password Cannot Be Blank";
    else{
       document.getElementById("loginform").submit();
    }


    <form id="loginform" action="login.do" method="post">
    UserName:<html:text property="userName"></html:text>
    PassWord:<html:password property="passWord"></html:password>
    <html:button onclick="validate()"></html:button>
    </form>

the above code is not working and I am unable to get the values in textbox for validation... Please help me out in this issue...

上面的代码不起作用,我无法在文本框中获取值进行验证...请帮我解决这个问题...

回答by Reddy

function validate(){
    var username = document.getElementById("userName").value;
    var password = document.getElementById("passWord").value;
}


<form id="loginform" action="login.do" method="post">
    UserName:<html:text property="userName" styleId="userName"></html:text>
    PassWord:<html:password property="passWord" styleId="passWord"></html:password>
    <html:button onclick="validate()"></html:button>
</form>

回答by Shashi

Before reading next line, please install good web inspector. e.g.

在阅读下一行之前,请安装好的网络检查器。例如

  • Firebug in Firefox
  • Firefox 中的萤火虫
  • Web Inspector in Chrome
  • Chrome 中的 Web Inspector
  • IE Web Inspector
  • IE网页检查器
  • and inspect the source

    并检查来源

    Or you can view the source.

    或者你可以查看源码。

       1. document.getElementsByName("userName")[0].value;
       2. document.getElementById("userName").value;
       3. document.forms["loginform"]["userName"].value