jquery ajaxform url 参数

发布于 2024-11-01 06:12:48 字数 381 浏览 0 评论 0原文

抱歉,这个愚蠢的问题...

我需要计算 ajaxForm() 的“url”参数...

我写了这样的东西...

$('#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"
        }
    }
}); 

但是 url 只接受字符串:-(

我该怎么办?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装纯掩盖桑 2024-11-08 06:12:48

我想你在这里运气不好。听起来您想根据表单元素之一的值更改表单的目标;在将 ajaxForm 绑定到表单之后,才会知道有问题的值,因此 Justin 的技巧不起作用。

您可以使用 beforeSubmitbeforeSerialize 挂钩来更改 ajaxForm URL,但 API 中没有任何内容允许您更改一次 URL ajaxForm 已绑定。所以,钩子对你来说没有任何用处。

您可以尝试向表单添加提交处理程序;该处理程序将检查 #form-domanda input[name=domanda_id] 的值,然后将适当的 ajaxForm 绑定到 #form-domanda 并在绑定ajaxForm后调用$('#form-domanda').ajaxSubmit()。这可能有效,也可能无效,坦率地说,这有点像黑客。

我认为您最好将 domanda_id 逻辑移至服务器上,无论是在 url-1.cfm 中还是在生成 #form-domanda< 的任何内容中/code> 首先。

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.

可是我不能没有你 2024-11-08 06:12:48

试试这个,立即调用该函数:

$('#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"
    }()
}
}); 

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"
    }()
}
}); 
动次打次papapa 2024-11-08 06:12:48

作为参考,这对我有用:

$('#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";
        }
    }
});

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";
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文