jquery ajax call for multiple button click events
The following is my generated html code. I'm having a bit of trouble coming up with what should be the jquery code to fire an ajax request upon a user clicking the activate button.
<span class="vId">
<input type="hidden" name="id" id="7" />
<input type="hidden" name="ClientID" id="026CC6D7-34B2-33D5-B551-CA31EB6CE345" />
<input class="textbox" type="text开发者_运维问答" name="key" />
<input class="button" type="button" name="Activate" value="Activate" />
</span>
<span class="gc_Name">Bartlett-White</span>
<span class="vId">
<input type="hidden" name="id" id="2" />
<input type="hidden" name="ClientID" id="000214EE-0000-0000-C000-000000000046" />
<input class="textbox" type="text" name="key" />
<input class="button" type="button" name="Activate" value="Activate" />
</span>
<span class="gc_Name">Landingham Bends</span>
<span class="vId">
<input type="hidden" name="id" id="8" />
<input type="hidden" name="ClientID" id="049F2CE6-D996-4721-897A-DB15CE9EB73D" />
<input class="textbox" type="text" name="key" />
<input class="button" type="button" name="Activate" value="Activate" />
</span>
<span class="gc_Name">Russell River</span>
My idea, is as follows:
<script type="text/javascript">
$().ready(function(){
$.each($(".button")
.click(function() {
$.ajax({
url: '{site_url}index.php/activate',
type: 'POST',
dataType: 'html',
data: {
key: $(this).sibling(':first'),
idclient: $(this).sibling(':first:next'),
},
success: function(result) {
}
});
});
)
});
</script>
While I know my jquery doesn't work, I'm a bit stumped...any help would be greatly appreciated. As i said, I'm attempting to send an ajax request off to the activate controller whenever a user clicks the activate button, however I also need to send the hidden data within the same span as well.
Once again thanks for any help.
<script type="text/javascript">
$().ready(function(){
$(".button").click(function() {
var button = this;
$.ajax({
url: '{site_url}index.php/activate',
type: 'POST',
dataType: 'html',
data: {
key: $(button).siblings('[name="id"]').attr("id"),
idclient: $(button).siblings('[name="ClientId"]').attr("id"),
},
success: function(result) {
}
});
});
});
</script>
$(".button").click(function(e) {
e.preventDefault();
var that = this;
$.ajax({
url: '{site_url}index.php/activate',
type: 'POST',
dataType: 'html',
data: {
key: $(that).prev().attr('id'),
idclient: $(that).siblings('[name="clientID"]').attr('id'),
},
success: function(result) {
alert(result);
$(that).blahblah();
}
});
});
- There is no need to loop over the buttons.
$(".button").click(...
will implicitly attach a handler to all elements with the 'button' class. - You need to assign the context to a variable in order that it is accessible within the callbacks/scope of the
$.ajax
call. - Ideally you should prevent form submission by making use of
event.preventDefault()
- Your traversing is incorrect, and there is no
:next
pseudo-selector as far as I know.
精彩评论