0

sorry for stupid question...

i need to calculate the "url" parameter of a ajaxForm()...

i write something like this...

$('#form-domanda').ajaxForm({
    url: function() {
        id = $('#form-domanda input[name=domanda_id]').val()
        if (id > 0) {
            return "url-1.cfm"
        } else {
            return "url-2.cfm"
        }
    }
}); 

but url accept only string :-(

how can i do?

3 Answers 3

1

I think you're out of luck here. Sounds like you want to change the form's target based on the value of one of the form's elements; the value in question won't be known until after ajaxForm is bound to the form so Justin's trick won't work.

You could use the beforeSubmit or beforeSerialize hooks to change the ajaxForm URL but there's nothing in the API that lets you change the URL once ajaxForm has been bound. So, the hooks are of no use to you.

You could try adding a submit handler to the form; this handler would check the value of #form-domanda input[name=domanda_id] and then bind the appropriate ajaxForm to #form-domanda and call $('#form-domanda').ajaxSubmit() after the you've bound ajaxForm. This might work or it might not and quite frankly, this is a bit of a hack.

I think you'd be better off moving the domanda_id logic onto the server, either in url-1.cfm or whatever is generating the #form-domanda in the first place.

Sign up to request clarification or add additional context in comments.

3 Comments

your are right... i'll change my server side script... many thanks!
Tks, it help me to solve this problem $('form#fileupload').ajaxSubmit({ beforeSubmit: function(data, form, options) { options["url"] = "your url here" }, success:function (data) { //do anything when success } });
@HuestonRido What problem? Perhaps you should ask a new question so that you have room to explain what's wrong.
1

Try this, to call the function immediately:

$('#form-domanda').ajaxForm({
url: function() {
    id = $('#form-domanda input[name=domanda_id]').val()
    if (id > 0) {
        return "url-1.cfm"
    } else {
        return "url-2.cfm"
    }()
}
}); 

Comments

1

For reference, this works for me:

$('#form-domanda').ajaxForm({
    beforeSubmit: function(data, form, options) {
        id = form.find('input[name=domanda_id]').val();
        if (id > 0) {
            options["url"] = "url-1.cfm";
        } else {
            options["url"] = "url-2.cfm";
        }
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.