我有一個兩個表單相互衝突 我想更改表單id在「var form = $('form');」如「var form = $('myForm');」 請讓我知道如果我改變ID形式myForm的,在下面的代碼這種情況下,我怎麼能合併:衝突兩個HTML表單
$(document).ready(function(){
var form = $('form');
var submit = $('#submit');
form.on('submit', function(e) {
// prevent default action
e.preventDefault();
// send ajax request
$.ajax({
url: 'ajax_comment.php',
type: 'POST',
cache: false,
data: form.serialize(), //form serizlize data
beforeSend: function(){
// change submit button value text and disabled it
submit.val('Submitting...').attr('disabled', 'disabled');
},
success: function(data){
// Append with fadeIn see http://stackoverflow.com/a/978731
var item = $(data).hide().fadeIn(800);
$('.comment-block').append(item);
// reset form and button
form.trigger('reset');
submit.val('Submit Comment').removeAttr('disabled');
},
error: function(e){
alert(e);
}
});
});
});
HTML代碼:
<form class="form-horizontal" role="form" id="form" method="post">
<!-- need to supply post id with hidden fild -->
<input type="hidden" name="filmId" value="<?php echo $row['filmId']?>">
<div class="form-group">
<div class="col-sm-2">
<label>Name *</label>
</div>
<div class="col-sm-10">
<input class="form-control" type="text" name="name" id="comment-name" placeholder="Your Name" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<label>Email *</label>
</div>
<div class="col-sm-10">
<input class="form-control"type="email" name="mail" id="comment-mail" placeholder="Your Email" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<label>Comment *</label>
</div>
<div class="col-sm-10">
<textarea class="form-control" rows="5" name="comment" id="comment" placeholder="Type your comment here...." required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" id="submit" value="Submit Comment">
</div>
</div>
</form>
到底是什麼衝突?現在你正在使用一個選擇器來選擇DOM中的任何表單元素,而不管ID如何。如果您想要定位特定表單,請定位該ID。 – APAD1 2014-12-03 15:35:59