views:

530

answers:

5

Here's the deal : in order to build a correct series of parameters, i'm trying to loop over all inputs included in an ajax built form; Code down here isn't finished, yet I guess you will understand it :

$("#formulaireUploadFichier").live('submit',function(e){
    var fm = $(this);
    var fld = [];
    $('input',fm).not('[type=submit]').each(function(){
        if ($(this"[type=radio]:checked"||$(this).not("[type=radio]"))
        var name= $(this).attr("name");
        var val= $(this).val();
        fld.push(name+'='+val);
    });
    //construct url (note : should really be encoded!)...
    var url = fm.attr('action')+'?'+fld.join('&');
    //open shadowbox...
    Shadowbox.open({
        player:"html",
        content:url,
        width:800,
        height:800
    });
    //prevent form submission...
    e.preventDefault();
}); 

The itchy part comes line 5, where i'm trying to write a correct selector for JQuery to pass over non checked/selected radio buttons. I know i'm not that far from it, but I'm getting messed up, so any help clearing this help would be greatly appreciated.

A: 

You've messed up something near $(this"[type

EDIT:

var inps=$('input');
var whatYouWant = inps.filter('[type!=radio]').add(inps.filter('[type=radio]:checked'));

this way it should do what You wanted

naugtur
This doesn't work. It returns a collection of radios that are not checked. That (according to his example) is what he wants.
patrick dw
Ok, now I see what's bad, but next time don't end Your negative comments with "This is what he wants" ;) I'm fixing the answer in edit
naugtur
Oops. Meant to say it isn't what he wants. Thanks for the correction. Hope I wasn't too negative. Giving you a +1 for settin' me straight!
patrick dw
A: 

What are your expected resutls here?

    $('input',fm).not('[type=submit]').each(function(){
    if ($(this"[type=radio]:checked"||$(this).not("[type=radio]"))
    var name= $(this).attr("name");
    var val= $(this).val();
    fld.push(name+'='+val);
     });

What I see here is that if it's checked or not a radio you want the conditions below to fire. Youre missing thought the {} to surround the code, all youre doing is setting the var name when you meet the condition. Do you mean to have it run all that code? like this?

    $('input',fm).not('[type=submit]').each(function(){
if ($(this"[type=radio]:checked"||$(this).not("[type=radio]")){
var name= $(this).attr("name");
var val= $(this).val();
fld.push(name+'='+val);
}

 });
Mech Software
Interesting this was downflaged yet the code clearly wont set that name variable based on what the original poster posted.
Mech Software
@Mech - Toward whom is that directed? You commented your own answer.
patrick dw
A: 

I'd suggest to replace if ($(this"[type=radio]:checked"||$(this).not("[type=radio]")) by if($(this).attr('type') !== 'radio' || $(this).val()=='on')

Daan
+3  A: 

Since you're already in a loop of your input elements, no need to start another. Just test for the proper filters on each item.

if($(this).is(":radio:checked,:not(:radio)") {
        var name= $(this).attr("name");
        var val= $(this).val();
        fld.push(name+'='+val);
}

jQuery's is() function lets you compare an element to a selector that you pass as a parameter.

patrick dw
I've just started on jQuery like 2 days ago so I may be wrong, but I believe this selects all radio buttons that are not checked. I think the user intends to select all inputs EXCEPT radio buttons that aren't checked.
Justen
The user is already iterating over the inputs. jQuery's `is()` function lets you test an element to see if it matches a given selector. It returns `true` or `false`.
patrick dw
Ah okay, looking at it now I get it. Select checked radio buttons and then select no radio buttons.
Justen
Yes, it will return 'true' for all that are either 'unchecked radios' or 'not radios'.
patrick dw
Correct, thanks for the tip. I guess I'll just have to work out selectors a bit, as syntaxt is soooo easy when you look at it.
pixelboy
A: 

What exactly do you want to select:

  • Just checked radio buttons, or
  • All checked radio buttons AND checkboxes?

Code for just checked radio buttons:

if($(this).is(":radio:checked")) {

Code for all checked inputs (checkboxes and radio buttons):

if($(this).is(":radio:checked, :not(:radio):checked")) {

I hope this is what you're after.

Anriëtte Combrink