开发者

assigning an array value to a hidden field in jquery

开发者 https://www.devze.com 2023-03-03 11:37 出处:网络
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 personemai

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] => max@max.com ) 
        [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') ?>'/>&nbsp;&nbsp;&nbsp;&nbsp;
<input type='hidden' name='eve开发者_StackOverflowntAction' value='add_booking'/>
<input type='hidden' name='event_id' value='<?php echo $EM_Event->id; ?>'/>

Thanks


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号