单击按钮时,jQuery 函数未从 JSF 调用

发布于 2025-01-04 01:56:59 字数 1450 浏览 1 评论 0原文

我有一个 jQuery 文件,其中包含函数。当我的页面加载时,我的脚本就会运行。意味着它被调用。这是我的 jQuery 文件。

(function($){

    var $firstPara = $('p').eq(1);
    $firstPara.hide();

    function checkValidation() {

        var labelElementArray = $("label.mandotary");
        var inputElementArray = $("input.mandotary");
        var selectElementArray = $("select.mandotary");

        $.each(inputElementArray, function(index, element){

            var text = $(element).text();


        }); //end of $.each(inputElementArray, fn)

        $.each(selectElementArray, function(index, element) {

            ////nothing

        }); //end of  $.each(selectElementArray, fn)

    } //end of function checkValidation()

})(jQuery); //end of (function($){}

我有 JSF 表单,其中有按钮。

<h:head>
    <title>Facelet Title</title>
    <h:outputScript library="javascripts" name="jquery.js"  target="body"/>
    <h:outputScript library="javascripts" name="validation.js" target="body"/>

</h:head>

<h:body>

    <h:form id="validationFrom" >

        <h:commandButton id="saveButton"
                         value="Save"
                         onclick="checkValidation()"/>


        <h:commandButton id="cancelButton"
                         value="Cancel" />

    </h:form >

</h:body>

现在,当我单击“保存”按钮时,它说 checkValidation() 函数未定义。为什么? 我在这里做错了什么?

谢谢

I have a jQuery file with function in it. When my page loads then my script runs. Means it's invoke. Here is my jQuery file.

(function($){

    var $firstPara = $('p').eq(1);
    $firstPara.hide();

    function checkValidation() {

        var labelElementArray = $("label.mandotary");
        var inputElementArray = $("input.mandotary");
        var selectElementArray = $("select.mandotary");

        $.each(inputElementArray, function(index, element){

            var text = $(element).text();


        }); //end of $.each(inputElementArray, fn)

        $.each(selectElementArray, function(index, element) {

            ////nothing

        }); //end of  $.each(selectElementArray, fn)

    } //end of function checkValidation()

})(jQuery); //end of (function($){}

I have JSF form with button in it.

<h:head>
    <title>Facelet Title</title>
    <h:outputScript library="javascripts" name="jquery.js"  target="body"/>
    <h:outputScript library="javascripts" name="validation.js" target="body"/>

</h:head>

<h:body>

    <h:form id="validationFrom" >

        <h:commandButton id="saveButton"
                         value="Save"
                         onclick="checkValidation()"/>


        <h:commandButton id="cancelButton"
                         value="Cancel" />

    </h:form >

</h:body>

Now when i click on save button, it says that checkValidation() function is undefined. Why?
What i am doing wrong here?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2025-01-11 01:56:59

尝试删除

(function($){

})(jQuery); //(函数($){}结束

从js文件中

,而是用这个地方包装你的js代码,

$(document).ready(function() {

});

一件事

再编辑

:将其添加到页面的头部,

    <h:outputScript library="primefaces" name="jquery/jquery.js"
        target="head" />
    <h:outputScript target="head">
        $ = jQuery;
    </h:outputScript>

以便在你的jQuery代码中使用$

try to remove the

(function($){

and the

})(jQuery); //end of (function($){}

from the js file

and instead , wrap your js code with this place the

$(document).ready(function() {

});

one more thing

EDIT:

add this in the head of your page

    <h:outputScript library="primefaces" name="jquery/jquery.js"
        target="head" />
    <h:outputScript target="head">
        $ = jQuery;
    </h:outputScript>

in order to use the $ in your jQuery code

森林散布 2025-01-11 01:56:59

好吧,除了我刚刚进行的少量谷歌搜索之外,我不知道 JSF 是什么,而且我也不熟悉您正在使用的一些 jQuery 语法。假设您可以像处理任何常规旧 html 页面一样处理 JSF 页面,下面是我的做法...

$(function() {
    // Do any work you want to occur when the DOM is ready here.

    $("#saveButton").click(checkValidation);
}

function checkValidation() {
    // Check validation here
} 

然后删除 JSF 中的 onclick 位。

Welp, I have no idea what JSF is other than the small amount of googling I just did and I'm also not familiar with some of the jQuery syntax you are using. Assuming you can work against a JSF page the same as any regular old html page, here's how I would do it...

$(function() {
    // Do any work you want to occur when the DOM is ready here.

    $("#saveButton").click(checkValidation);
}

function checkValidation() {
    // Check validation here
} 

Then get rid of your onclick bit in the JSF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文