将数组值分配给jquery中的隐藏字段

发布于 2024-11-05 20:21:56 字数 2091 浏览 0 评论 0原文

当然我在这里缺少一些简单的东西。我正在尝试通过 jquery 为隐藏字段分配一个值。它一直不返回任何内容,我已经尝试了几种方法。我在名为 personemail[] 的表单上有一个数组,它对字段进行 id 和命名,如 personemail[0]、personemail[1] 等,我想提取列出的第一个电子邮件地址并将其分配给名为 person_email 的隐藏字段。

    <script type="text/javascript" charset="utf-8">
    jQuery(document).ready(function() {
    var emailvalue = jQuery('#personemail[0]').val();
    jQuery('#person_email').val(emailvalue);
    });
</script>

我是 jquery 新手,所以这对我所缺少的东西有点神秘。我需要在 #emailperson[] 上使用 .change() 吗?我假设我需要闯入数组来获取值,但我不确定如何进行。我就是无法让这件作品发挥作用。

这是帖子数据

Array ( [person_name] => max 
        [person_email] => 
        [person_phone] => 111-1111 
        [booking_seats] => 1 
        [personname] => Array ( [0] => max ) 
        [personemail] => Array ( [0] => [email protected] ) 
        [booking_comment] => 
        [eventAction] => add_booking 
        [event_id] => 14 )

正如你所看到的,我将电子邮件地址很好地放入数组中,我只是无法让它坚持到 person_email

这里是要求的 HTML 配对版本,服务器端语言是 php。输入是通过我编写的 jquery 克隆函数生成的。根据所选人数的下拉列表为每个新人吐出一个 html 元素:

<form id='rsvp-form' name='booking-form' method='post' action='<?php echo $destination ?>'>          
    <tr class="personrow">
    <th scope="row"></th><td>Name:<br><input class="required person" type="text" name="personname[0]" /><br>Email:<br><input class="required email additional" type="text" name="personemail[0]" id="personemail[0]" /></td><td class="theextra"></td>
</tr>       
<input type='hidden' name='person_email' id='person_email' />
<input type='submit' class='blkBTN' value='<?php _e('Book It!', 'dbem') ?>'/>&nbsp;&nbsp;&nbsp;&nbsp;
<input type='hidden' name='eventAction' value='add_booking'/>
<input type='hidden' name='event_id' value='<?php echo $EM_Event->id; ?>'/>

谢谢

Sure I'm missing something simple here. I'm trying to assign a value to a hidden field via jquery. It keeps returning nothing and I've tried several things. I have an array on a form called personemail[] and it ids and names the fields accordingly, personemail[0], personemail[1] ect and I want to extract the first email address listed and assign it to a hidden field named person_email.

    <script type="text/javascript" charset="utf-8">
    jQuery(document).ready(function() {
    var emailvalue = jQuery('#personemail[0]').val();
    jQuery('#person_email').val(emailvalue);
    });
</script>

I'm new to jquery so this is a bit of a mystery as to what I'm missing. do I need to be using a .change() on the #emailperson[]? I'm assuming I need to break into the array to grab the value but I'm unsure how. I just can't get this piece to work.

Here is the post data

Array ( [person_name] => max 
        [person_email] => 
        [person_phone] => 111-1111 
        [booking_seats] => 1 
        [personname] => Array ( [0] => max ) 
        [personemail] => Array ( [0] => [email protected] ) 
        [booking_comment] => 
        [eventAction] => add_booking 
        [event_id] => 14 )

As you can see I get the email address into the array just fine, I just can't get it to stick to person_email

Here is a paired down version of HTML asked for, server side language is php. The inputs are generated via a jquery clone function I've written. Spits out a html element for each new person based on dropdown for the number of people selected:

<form id='rsvp-form' name='booking-form' method='post' action='<?php echo $destination ?>'>          
    <tr class="personrow">
    <th scope="row"></th><td>Name:<br><input class="required person" type="text" name="personname[0]" /><br>Email:<br><input class="required email additional" type="text" name="personemail[0]" id="personemail[0]" /></td><td class="theextra"></td>
</tr>       
<input type='hidden' name='person_email' id='person_email' />
<input type='submit' class='blkBTN' value='<?php _e('Book It!', 'dbem') ?>'/>    
<input type='hidden' name='eventAction' value='add_booking'/>
<input type='hidden' name='event_id' value='<?php echo $EM_Event->id; ?>'/>

Thanks

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

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

发布评论

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

评论(2

药祭#氼 2024-11-12 20:21:57

在我看来,您正在尝试在提交之前准备好文档上的输入数据。在那一刻,输入字段中没有任何内容可供读取。我要做的就是在提交时执行此操作,如下所示这

$("#rsvp-form").submit(function(event){
    event.preventDefault(); //stop the form from submitting, you'll do it later
    var emailvalue = $("personemail\\[0\\]").val();
    $("#person_email").val(emailvalue);
    $("#rsvp-form").submit(); //you got the hidden input value, now submit the form
});

是一个 jsfiddle 示例 http://jsfiddle.net/x6dPd/< /a>

编辑:示例中存在拼写错误。

It seems to me that you're trying to get input data on document ready - before it was submitted. In that moment there's nothing in the input field to read from. What I'd do is do it onsubmit, something like this

$("#rsvp-form").submit(function(event){
    event.preventDefault(); //stop the form from submitting, you'll do it later
    var emailvalue = $("personemail\\[0\\]").val();
    $("#person_email").val(emailvalue);
    $("#rsvp-form").submit(); //you got the hidden input value, now submit the form
});

Here's a jsfiddle example http://jsfiddle.net/x6dPd/

edit: there was a typo in the example.

_畞蕅 2024-11-12 20:21:57

您应该转义选择器中的括号(使用反斜杠),因为括号是 jquery 选择器中的特殊字符。所以你应该:

var emailvalue = jQuery('#personemail\\[0\\]').val();

希望这有帮助。干杯。

You should escape brackets in your selector (with backslashes), because brackets are special characters in jquery selectors. So you should have:

var emailvalue = jQuery('#personemail\\[0\\]').val();

Hope this helps. Cheers.

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