获取参数错误编号
无法将 javascript 变量传递给 http get 请求。这是获取 select 标记的值并将其传递到 CoffeeScript 中的请求的代码块:
jQuery(document).ready ->
mktuClassID = $("#mktu_class").val();
$("#mktu_class").change getClassID = ->
mktuClassID = $(this).val()
$("#registration_application_items").tokenInput("/items/" + mktuClassID, {
crossDomain: false,
prePopulate: $("#registration_application_items").data("pre"),
preventDuplicates: true,
theme: "facebook"
})
它始终在 mktuClassID 变量中发送相同的值,但是,我看到我的值在 console.log() 中发生了更改,但它始终是在 tokenInput 方法中传递相同的值。 这是日志:
Processing by MktuItemsController#token_inputs as JSON
Parameters: {"q"=>"2", "class_id"=>"3"}
MktuItem Load (0.7ms) SELECT "mktu_items".* FROM "mktu_items" WHERE (name like '%2%' AND mktu_class_id = '3')
请帮助我,我应该如何重新排列我的代码才能使其工作?
Can not pass javascript variable to http get request. Here is the block of code that takes the value of select tag and passes it into request in coffeescript:
jQuery(document).ready ->
mktuClassID = $("#mktu_class").val();
$("#mktu_class").change getClassID = ->
mktuClassID = $(this).val()
$("#registration_application_items").tokenInput("/items/" + mktuClassID, {
crossDomain: false,
prePopulate: $("#registration_application_items").data("pre"),
preventDuplicates: true,
theme: "facebook"
})
it is always sending the same value in mktuClassID variable, however, I see that my value changed in console.log() but it is always passing the same value in tokenInput method.
Here is the log:
Processing by MktuItemsController#token_inputs as JSON
Parameters: {"q"=>"2", "class_id"=>"3"}
MktuItem Load (0.7ms) SELECT "mktu_items".* FROM "mktu_items" WHERE (name like '%2%' AND mktu_class_id = '3')
Please help me, how do I should rearrange my code to get it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
ready
处理程序会执行三件事:#mktu_class
的当前值并将其存储在mktuClassID
中。#mktu_class
,该处理程序将其值存储在mktuClassID
中。"/items/" + mktuClassID
调用tokenInput
,其中mktuClassID
是 (1) 中的值。结果是
#mktu_class
上的更改处理程序不会执行任何有用的操作,并且对#mktu_class
的任何更改都将被忽略(好吧,它们最终会在>mktuClassID
但没有任何内容会查看mktuClassID
,因此效果是相同的)。也许您的缩进已关闭,并且您确实希望/items
调用位于更改处理程序内:如果是这种情况,则完全取消
mktuClassID
并添加>$('#mktu_class').change()
调用来初始化:当您将
mktuClassID
放入 URL 时,您确实应该保持偏执并对其进行编码,因此我添加了 <一个href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent" rel="nofollow">encodeURIComponent
也调用。如果您只想在
#mktu_class
第一次更改时调用tokenInput
,则将更改处理程序与one
:该版本仅在第一次
#mktu_class
更改时调用tokenInput
。Your
ready
handler does three things:#mktu_class
and stores it inmktuClassID
.#mktu_class
which stores its value inmktuClassID
.tokenInput
with a URL of"/items/" + mktuClassID
wheremktuClassID
is the value from (1).The result is that your change handler on
#mktu_class
doesn't do anything useful and any changes to#mktu_class
will be ignored (well, they'll end up inmktuClassID
but nothing will look atmktuClassID
so the effect is the same). Perhaps your indentation is off and you really want the/items
call to be inside the change handler:If that's the case, then do away with
mktuClassID
entirely and add a$('#mktu_class').change()
call to initialize things:And you really should be properly paranoid and encode
mktuClassID
when you put it in a URL so I added anencodeURIComponent
call too.If you only want to call
tokenInput
the first time that#mktu_class
changes then bind the change handler withone
:That version would call
tokenInput
only the first time#mktu_class
changed.