jQuery对话框-外部链接的JavaScript无法正常工作 [英] jquery dialog - external link's javascript not working

查看:79
本文介绍了jQuery对话框-外部链接的JavaScript无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个对话框,该对话框将使用一个表单来加载一个外部php文件,该表单要求提供信息,包括使用jQuery datepicker和javascript代码的日期,以便在提交前检查所有字段是否已填写.我有正确弹出的对话框,但是外部php文件中的javascript不起作用(日历不弹出,并且表单验证检查不起作用).我使用的是外部php文件,因为我已将链接设置为传递变量以填写表单的某一部分.我不确定这些信息是否有用,但是将php文件加载到正常的窗口链接中可以很好地工作,只有当对话框中的javascript无法正常工作时.

I'm trying to make a dialog box that loads an external php file with a form that asks for information including a date which uses the jQuery datepicker and a javascript code to check if all the fields are filled in before submitting. I have the dialog box popping up correctly however the javascript in the external php file do not work(calendar doesn't pop up and the form validation checks aren't working). I am using an external php file because I have the link set to pass a variable to fill in one section of the form. I'm not sure if this information will be useful but loading the php file in a normal window link works perfectly fine, it is only when it is in the dialog that the javascript is not working.

这是对话框的脚本:

<script type="text/javascript">
$(document).ready(function() {
    $('#order a').each(function() {
        var $link = $(this);
        var $dialog = $('<div></div>')
        .load($link.attr('href') + ' #content')
        .dialog({
            autoOpen: false,
            title: $link.attr('title'),
            width: 500,
            height: 400
        });

        $link.click(function(){
            $dialog.dialog('open');
            return false;
        });
    });
});
</script>

这是我弹出对话框的代码:

This is the code I have that pops up the dialog:

<div id="order">
    <a href="package.php?code=NY1" title="Package Booking"><img src="images/order.png"></a>
</div>

php文件中的javascript代码和表单:

The javascript code and form in the php file:

<head>
<script type="text/javascript">
$(function(){
            // Datepicker
            $('#pdate').datepicker({
                inline: true,
                minDate: 0,
            });
</script>
<script type="text/javascript">
function packageValidator(){
    var pdate = document.getElementById('pdate');
                    if(notEmpty(pdate, "Please enter a departure date.")){
                        return true;
                    }

    return false;
}
function notEmpty(elem, helperMsg){
    if(elem.value.length == 0){
        alert(helperMsg);
        elem.focus();
        return false;
    }
    return true;
}
</script>
</head>
<body>
<div id="content">
<form action="send_package.php" method="post" onsubmit='return packageValidator()' >
        <table>
          //Form Information
            <tr>
                <td><input id="pdate" name="pdate" type="text"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
</form>
</div>
</body>

推荐答案

我从未见过这样的设置.不知道是否可以发布到仅包含javascript的页面. 在带有表单的页面上,您必须将PHP文件中的所有javascript都放入一个外部.js文件中,并添加

I've never seen a setup like this one. Not sure if you can post to a page containing only javascript in it. On the page with the form, you have to throw all that javascript from the PHP file into an external .js file and add

<script src="myjavascript.js"></script>

到带有表单的页面.

此外,当您将javascript放在外部文件中时,请删除标签. 然后,您可以在表单中运行验证,并调用在对话框中呈现链接的函数.

Also, when you put the javascript in the external file, remove the tags. Then you can run your validation within the form, and call the functions that render the links in the dialog box.

这篇关于jQuery对话框-外部链接的JavaScript无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆