Jquery表单从字段值获取

发布于 2024-12-11 21:40:29 字数 1203 浏览 0 评论 0原文

我在我的网站上使用 Jquery 表单。发件人非常简单,包含常用字段(姓名、电子邮件、出生日期等)。用户按下注册按钮后,表单将使用 Jquery 的“ajaxForm”参数提交到 php 文件。之后,表单滑出,谷歌地图滑入,要求客户通过拖动地图上的标记来选择他当前的位置。

将标记放置在所需位置后,客户可以单击“提交”按钮,两个值将传递到另一个 php 文件:纬度和经度,即坐标。 (请注意,页面不会随时刷新,这一切都发生在一个页面上。)

我想要做的是将前一个表单中的电子邮件字段值与纬度和经度一起传递,但我真的不能这样做因为我对 javascript 几乎是个菜鸟。

有没有办法在提交表单之前从“电子邮件”字段声明一个全局变量,以便我稍后可以使用它与纬度和经度参数一起传递。或者也许有更简单的解决方案?

预先非常感谢您!

这是我的 jquery 表单的代码

$(document).ready(function() {
var options = {
    target: '#total',
    clearForm: true,
    beforeSubmit: radio,
    success: social1
};
$("#perm").ajaxForm(options);

}); 
function radio(formData, jqForm, options) {

    if ($('input:radio').is(':checked')) {

} else {
    alert('choose gender!');
    return false;
}

var form = jqForm[0];

        if (form.year.value <= form.yob.value) { 
        alert('choose another year!'); 
        return false; 
    }

};




function social1() {  
            $("#map").delay(700).slideDown("slow");
            $("#accordion").slideUp("slow");
            };

这是按钮代码

<form id='perm' action='add.php?stay=perm' method='post' onSubmit="return checkForm(this);">

I'm using a Jquery form on my website. The from is pretty much simple with the usual fields (name, email, dob etc.). After user presses register button, the form is submitted to the php file using Jquery from's 'ajaxForm' parameter. After that, the form slides out and the google map slides in asking customer to select his current location by dragging the marker on the map.

After the marker is put in the required place, customer can click "Submit" button and 2 values will be passed to another php file : latitude and longitude, ie coordinates. (please note that page does not refreshes at any time, this all happening on one page.)

What I want to do is to pass the email field value from the previous form together with latitude and longitude, but I can't really do that because I'm pretty much a noob at javascript.

Is there a way to declare a global variable from the field 'email' before submitting the form, so I could later use it to pass together with the latitude and longitude parameters. Or maybe there's even easier solution?

Thank you very much in advance!

here's the code of my jquery form

$(document).ready(function() {
var options = {
    target: '#total',
    clearForm: true,
    beforeSubmit: radio,
    success: social1
};
$("#perm").ajaxForm(options);

}); 
function radio(formData, jqForm, options) {

    if ($('input:radio').is(':checked')) {

} else {
    alert('choose gender!');
    return false;
}

var form = jqForm[0];

        if (form.year.value <= form.yob.value) { 
        alert('choose another year!'); 
        return false; 
    }

};




function social1() {  
            $("#map").delay(700).slideDown("slow");
            $("#accordion").slideUp("slow");
            };

And here's the button code

<form id='perm' action='add.php?stay=perm' method='post' onSubmit="return checkForm(this);">

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

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

发布评论

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

评论(2

温暖的光 2024-12-18 21:40:29

您已经在使用 beforeSubmit 方法。稍微展开一下。这是捕获电子邮件地址的好位置。所以你最终会遇到这样的情况(未经测试):

var email;
$(document).ready(function() {
var options = {
    target: '#total',
    clearForm: true,
    beforeSubmit: radio,
    success: social1
};
$("#perm").ajaxForm(options);

}); 
function radio(formData, jqForm, options) {
   email = $("#idOfEmailField").val();
   // ... rest of logic here
};
function social1() {  
   alert("Email address is "+email);
};

You are already using the beforeSubmit method. Expand it a little bit. It is a fine location to catch the email address. So you end up with a situation like this (untested):

var email;
$(document).ready(function() {
var options = {
    target: '#total',
    clearForm: true,
    beforeSubmit: radio,
    success: social1
};
$("#perm").ajaxForm(options);

}); 
function radio(formData, jqForm, options) {
   email = $("#idOfEmailField").val();
   // ... rest of logic here
};
function social1() {  
   alert("Email address is "+email);
};
祁梦 2024-12-18 21:40:29

如果您只是隐藏表单而不刷新页面,则先前表单输入的电子邮件仍然可用(即您不需要声明全局变量来存储电子邮件)。例如,

<form id='perm' action='add.php?stay=perm' method='post'>
    ...
    <input type="text" id="name" />
    <input type="text" id="email" />
    <input type="submit" value="Submit" />
    ...
</form>

<button id='submitCoordinate_button' style='display:none'>Submit coordinate</button>

<script type="text/javascript">
    $('#perm').ajaxForm() {
        ...
        success: function() {
            $('#perm').hide(); // hide the perm form and pull the map in 
            $('#submitCoordinate_button').show();
        },
        ...
    }

    $('#submitCoordinate_button').click(function () {
        ...
        var email = $('#email').val(); // you still can get the email here
        ...
    });
</script>

If you simply hide the form without refreshing the page, the email input from the previous form is still available (i.e you don't need to declare a global variable to store the email). For example,

<form id='perm' action='add.php?stay=perm' method='post'>
    ...
    <input type="text" id="name" />
    <input type="text" id="email" />
    <input type="submit" value="Submit" />
    ...
</form>

<button id='submitCoordinate_button' style='display:none'>Submit coordinate</button>

<script type="text/javascript">
    $('#perm').ajaxForm() {
        ...
        success: function() {
            $('#perm').hide(); // hide the perm form and pull the map in 
            $('#submitCoordinate_button').show();
        },
        ...
    }

    $('#submitCoordinate_button').click(function () {
        ...
        var email = $('#email').val(); // you still can get the email here
        ...
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文