I think I must be missing something when it comes to $.post a form in
jQuery. It seems too complicated to have to do something like this:
$.post("delete.php",{
field1: $("#field1").val(),
field2: $("#field2").val(),
field3: $("#field3").val(),
field4: $("#field4").val(),
}
Isn't there a way to simply post the whole form, with the whole post
collection, to the specified url, without having to specify which
field should be collected? That would make dealing with checkboxes
much easier...
Here's a sample form. How would you submit it?
<form name="test" id="test" method="post" action="script.php">
<input type="text" name="field1" id="field1" value="" />
<input type="text" name="field2" id="field2" value="" />
<input type="text" name="field3" id="field3" value="" />
<input type="text" name="field4" id="field4" value="" />
<input type="checkbox" name="city[]" class="cities" value="1" />
<input type="checkbox" name="city[]" class="cities" value="2" />
<input type="checkbox" name="city[]" class="cities" value="3" />
<input type="checkbox" name="city[]" class="cities" value="4" />
<input type="checkbox" name="city[]" class="cities" value="5" />
Regards,
-jj.
On 31 déc 2008, 22:56, Ricardo Tomasi <ricardob...@gmail.com> wrote:
> Assuming you have unique names:
>
> var CheckedIDs = [];
>
> $("input.items_id").livequery('click',function(event){
> $("input.items_id").each(function() {
> if (this.checked) { CheckedIDs[this.name] = $(this).attr
> ("value"); }
> else { CheckedIDs[this.name] = null }
> });
> });
>
> Why do you need to store them in an array anyway? The checked ones
> values will be submitted with the form, and you can access them at
> anytime with $(":checkbox:checked")
>
> On Dec 31, 3:52 pm, jjshell <blahblahcoui...@gmail.com> wrote:
>
>
>
> > Ok I'm getting closer... And thanks again for your help :)
>
> > I can manage to have the checked checkboxes added to the CheckedIDs
> > array. However, if I uncheck a checkbox, it is not removed from the
> > array.
>
> > It's the last little problem I have to solve :)
>
> > Here's my code:
>
> > var CheckedIDs = [];
>
> > $("input.items_id").livequery('click',function(event){
> > $("input.items_id").each(function() {
> > if(this.checked){CheckedIDs.push($(this).attr("value"));}
> > });
> > //alert('clicked');
> > });
>
> > On 31 déc, 17:33, Joe <joseph.is...@gmail.com> wrote:
>
> > > I don't believe you are using livequery in the proper way. You're
> > > passing an 'each' event, which does not exist here.
>
> > > In theory, you could do the following:
>
> > > $("input.item_id").livequery('foo',function(bar) {
> > > alert('nothing happens');
>
> > > });
>
> > > In the console, you will see the length of the wrapped set, the number
> > > of inputs with class "item_id".
>
> > > Check the API again: http://brandonaaron.net/docs/livequery/#api
>
> > > Also, $.each method is similar to a for-loop: http://docs.jquery.com/Core/each
>
> > > That's a start for sure...
>
> > > Cheers.
>
> > > Joe
>
> > >http://www.subprint.com
>
> > > On Dec 31, 9:39 am, jjshell <blahblahcoui...@gmail.com> wrote:
>
> > > > The problem seems to be located around these parts:
>
> > > > var CheckedIDs = [];
> > > > $("input.item_id").livequery('each',function(add) {
> > > > if (this.checked){
> > > > alert('push');
> > > > CheckedIDs.push($(this).attr("value"));
> > > > }
>
> > > > });
>
> > > > On 31 déc, 15:26, jjshell <blahblahcoui...@gmail.com> wrote:
>
> > > > > Thanks for your reply :)
>
> > > > > I only get the alert nothing selected though... Tried to go through
> > > > > the code, couldn't find what is wrong...
>
> > > > > Regards,
>
> > > > > -jj.
>
> > > > > On 31 déc, 14:51, MorningZ <morni...@gmail.com> wrote:
>
> > > > > > Not sure if it's the *best* way, but it works
>
> > > > > > I'd do something like
>
> > > > > > var CheckedIDs = [];
> > > > > > $("input.item_id").each(function() {
> > > > > > if (this.checked) { CheckedIDs.push($(this).attr("value")); }});
>
> > > > > > if (CheckedIDs.length == 0) {
> > > > > > alert("Nothing selected!");}
>
> > > > > > else {
> > > > > > $.post(
> > > > > > "delete.php",
> > > > > > item_ids: CheckedIDs.join(","),
> > > > > > function(data) {
> > > > > > //Handle the returned results
> > > > > > }
>
> > > > > > }
>
> > > > > > "delete.php" will see a comma delimited string of IDs to delete
>
> > > > > > On Dec 31, 7:11 am, jjshell <blahblahcoui...@gmail.com> wrote:
>
> > > > > > > Hi,
>
> > > > > > > I have a tabular data I need to be able to delete by selecting
> > > > > > > checkboxes, and submitting a form (a .php script then checks which
> > > > > > > checkboxes have been submitted, delete by id, return an updated list
> > > > > > > of the items injected into the dom).
>
> > > > > > > My problem is that I currently can't manage to gather these checkboxes
> > > > > > > values as an array.
>
> > > > > > > Please consider the below code:
>
> > > > > > > $(document).ready(function(){
>
> > > > > > > //submit the form
> > > > > > > $("form#form-delete").livequery('submit', function(event){
> > > > > > > $("span#wait").html('<img name="wait" src="wait.jpg" />');
> > > > > > > $.post("delete.php",{
> > > > > > > item_id: $(".item_id").val(),
> > > > > > > },function(data){
> > > > > > > $("span#wait").html('');
> > > > > > > $("div#view-items").html(data);
> > > > > > > });
> > > > > > > return false;
> > > > > > > });
>
> > > > > > > });
>
> > > > > > > And the html:
>
> > > > > > > <input name="item_id" class="item_id" value="1" type="checkbox">
> > > > > > > <input name="item_id" class="item_id" value="2" type="checkbox">
> > > > > > > <input name="item_id" class="item_id" value="3" type="checkbox">
> > > > > > > <input name="item_id" class="item_id" value="4" type="checkbox">
> > > > > > > //etc.
>
> > > > > > > What would be the ideal jQuery way to handle checkboxes?
>
> > > > > > > Thanks in advance for your time :)
>
> > > > > > > Regards,
>
> > > > > > > -jj.- Masquer le texte des messages précédents -
>
> > > > > > - Afficher le texte des messages précédents -- Masquer le texte des messages précédents -
>
> > > > > - Afficher le texte des messages précédents -- Masquer le texte des messages précédents -
>
> > > - Afficher le texte des messages précédents -- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -
No comments:
Post a Comment