简体   繁体   English

从 JSP 到 Javascript 检索值

[英]Retrieve values from JSP to Javascript

Below is the javascript on my JSP page:下面是我的 JSP 页面上的 javascript:

<SCRIPT language="JavaScript">
function checkPass(){

var pass1=request.getParameter('password');
var pass2=request.getParameter('password2');

    if (pass1==pass2){
        window.alert("YES");
    }
    else{
        window.alert("NO");
    }
    }
</SCRIPT>

Below is my form input text boxes:下面是我的表单输入文本框:

                    <h:outputLabel value="Password:" for="password" />
 <h:inputSecret id="password" value="#{StaffController.staff.password}" 
            size="20" required="true"
            label="Password" >
            <f:validateLength minimum="8" />
        </h:inputSecret>

        <h:message for="password" style="color:red" />

                    <h:outputLabel value="Password:" for="password2" />
 <h:inputSecret id="password2" 
            size="20" required="true"
            label="Password" >
            <f:validateLength minimum="8" />
        </h:inputSecret>
 <h:message for="password2" style="color:red" />

Below is my commandbutton linked with onmousedown to call the script when clicked:下面是与 onmousedown 链接的命令按钮,用于在单击时调用脚本:

<h:commandButton action="#{StaffController.saveUser()}" onmousedown="checkPass();" value="Submit"/>

I can't seem to retrieve value from my user input form.我似乎无法从我的用户输入表单中检索值。

  1. JSF changes id of components - view source of your page in browser. JSF 更改组件的 ID - 在浏览器中查看页面的源代码。 It should be something like a combination of form id+component id应该是表单id+组件id的组合

  2. Submit command invokes onmousedown event before making submit, so in checkPass you do not have a request object提交命令在提交之前调用 onmousedown 事件,所以在 checkPass 中你没有请求 object

  3. Better to write a custom validator for checking password最好编写一个自定义验证器来检查密码

First you require to set in the h:form the value false to the "prependId" attribute to make possible to get the textboxes by id on javascript.首先,您需要在 h:form 中将值 false 设置为“prependId”属性,以便通过 javascript 上的 id 获取文本框。 Is not recomended to remove the prependId .不建议删除prependId But it makes to you easier to work with javascript.但它使您更容易使用 javascript。 In other way you could use a field name or css name an other kind of selection (like using Jquery $('.cssname') or $('#cssid')以其他方式,您可以使用字段名称或 css 名称其他类型的选择(例如使用 Jquery $('.cssname') 或 $('#cssid')

Now you need to change the javascript.现在您需要更改 javascript。 javascript is performed on the client side. javascript 在客户端执行。 You can't get data from the request because the request is not done when the javascript is executed.您无法从请求中获取数据,因为执行 javascript 时请求未完成。

You need to get the inputSecret object,that in the html is an input item, using something like this:您需要获取 inputSecret object,在 html 中是一个输入项,使用如下内容:

var pass1 = document.getById('password').value;
var pass2 = document.getById('password2').value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM