jquery ajaxform url 参数
抱歉,这个愚蠢的问题...
我需要计算 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你在这里运气不好。听起来您想根据表单元素之一的值更改表单的目标;在将
ajaxForm
绑定到表单之后,才会知道有问题的值,因此 Justin 的技巧不起作用。您可以使用
beforeSubmit
或beforeSerialize
挂钩来更改ajaxForm
URL,但 API 中没有任何内容允许您更改一次 URLajaxForm
已绑定。所以,钩子对你来说没有任何用处。您可以尝试向表单添加提交处理程序;该处理程序将检查
#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
orbeforeSerialize
hooks to change theajaxForm
URL but there's nothing in the API that lets you change the URL onceajaxForm
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 appropriateajaxForm
to#form-domanda
and call$('#form-domanda').ajaxSubmit()
after the you've boundajaxForm
. 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 inurl-1.cfm
or whatever is generating the#form-domanda
in the first place.试试这个,立即调用该函数:
Try this, to call the function immediately:
作为参考,这对我有用:
For reference, this works for me: