我试图建立一个RSVP形式为我的婚礼,让你可以克隆几个字段的在时间(RSVP为多个客户http://adrianandemma.com/ )。 这些克隆领域包括“参加是/否”一对夫妇的单选按钮。
当我克隆我试图增加从他们的名字name属性中的单选按钮= “未来[0]” 来命名= “未来[1]”,名字= “未来[2]”,名字=“未来[3] ”等等。
我需要尝试做到这一点,因为每个克隆单选按钮,都需要有一个唯一的名称属性,否则你不能选择多个是/在同一时间没有答案。 这意味着我不能只用一个空数组名称=“未来[]”。
我想我部分地存在,因为当我克隆目前字段我的JS被修正的每个克隆字段NAME =“未来[0] [1]”,名字=“未来[0] [2]”,名字= “未来[0] [3]”。
我只需要尝试,并得到它删除[0],但不能工作了如何做到这一点使用jQuery。
这是我的HTML表单:
<form action="?" method="post">
<?php if(@$errors) :?>
<p class="errors"><?php echo $errors; ?></p>
<?php endif; ?>
<input type="hidden" name="submit" value="1" />
<div class="form-row">
<div class="field-l">
<p>Name</p>
</div>
<div class="field-r">
<p>Attending?</p>
</div>
</div>
<div class="form-row guest">
<div class="field-l">
<input type="text" name="name[0]" id="name" value="<?php echo htmlspecialchars(@$_REQUEST['name']); ?>" tabindex="1" />
</div>
<div class="field-r">
<input type="radio" name="coming[0]" id="coming-yes" class="coming-yes" value="Yes"><label for="coming-yes">Yes</label><input type="radio" name="coming[0]" id="coming-no" class="coming-no" value="No"><label for="coming-no">No</label>
</div>
</div>
<a class="addguest" href="#">Add further guest</a>
<div class="form-row">
<button type="submit" id="rsvp-submit" tabindex="2">Submit RSVP</button>
</div>
</form>
这是我的jQuery的:
$(document).ready(function() {
$('.addguest').on('click', function(e) {
e.preventDefault();
//
// get the current number of ele and increment it
//
var i = $('.guest').length + 1;
$('.guest').first().clone().find("input").attr('id', function(idx, attrVal) {
return attrVal + i; // change the id
}).attr('name', function(idx, attrVal) {
return attrVal+'['+i+']'; // change the name
}).val('').removeAttr('checked').end().find('label').attr('for', function(idx, attrVal) {
return attrVal + i; // change the for
}).end().insertBefore(this);
});
});
这里是我似乎并不通过单选按钮值来拉动型的方法的代码:
<?php
$name = $_POST['name'];
$coming = $_POST['coming'];
$errors = "";
if(!@$_POST['name']) { $errors .= "Please enter your name.<br/>\n"; }
if(!@$_POST['coming']) { $errors .= "Please enter yes or no for attending.<br/>\n"; }
if(@$_POST['emailaddress'] != '') { $spam = '1'; }
if (!$errors && @$spam != '1')
{
$to = "example@xyz.com";
$subject = "Wedding RSVP";
$headers = "From: noreply@adrianandemma.com";
$body = "The following RSVP has been sent via the website.\n\n";
for($i=0; $i < count($_POST['name']); $i++) {
$body .= "
Name ".($i+1)." : " . $_POST['name'][$i] . "\n
Coming ".($i+1)." : " . $_POST['coming'][$i] ."\n\n";
}
$body .= "\n\nDate Received: " . date("j F Y, g:i a") . "\n";
mail($to,$subject,$body,$headers);
}
?>