获取参数错误编号

发布于 2024-12-25 23:19:17 字数 885 浏览 2 评论 0原文

无法将 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 技术交流群。

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

发布评论

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

评论(1

夜无邪 2025-01-01 23:19:17

您的 ready 处理程序会执行三件事:

  1. 获取 #mktu_class 的当前值并将其存储在 mktuClassID 中。
  2. 将更改处理程序绑定到 #mktu_class,该处理程序将其值存储在 mktuClassID 中。
  3. 使用 URL "/items/" + mktuClassID 调用 tokenInput,其中 mktuClassID 是 (1) 中的值。

结果是 #mktu_class 上的更改处理程序不会执行任何有用的操作,并且对 #mktu_class 的任何更改都将被忽略(好吧,它们最终会在 >mktuClassID 但没有任何内容会查看 mktuClassID,因此效果是相同的)。也许您的缩进已关闭,并且您确实希望 /items 调用位于更改处理程序内:

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 并添加 >$('#mktu_class').change() 调用来初始化:

jQuery(document).ready ->
    $('#mktu_class').change ->
        mktuClassID = $(this).val()
        $('#registration_application_items').tokenInput('/items/' + encodeURIComponent(mktuClassID), {
            crossDomain: false,
            prePopulate: $('#registration_application_items').data('pre'),
            preventDuplicates: true,
            theme: 'facebook'
        })
    $('#mktu_class').change();

当您将 mktuClassID 放入 URL 时,您确实应该保持偏执并对其进行编码,因此我添加了 <一个href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent" rel="nofollow">encodeURIComponent 也调用。

如果您只想在 #mktu_class 第一次更改时调用 tokenInput,则将更改处理程序与 one

jQuery(document).ready ->
    $('#mktu_class').one 'change', ->
        mktuClassID = $(this).val()
        $('#registration_application_items').tokenInput('/items/' + encodeURIComponent(mktuClassID), {
            crossDomain: false,
            prePopulate: $('#registration_application_items').data('pre'),
            preventDuplicates: true,
            theme: 'facebook'
        })

该版本仅在第一次 #mktu_class 更改时调用 tokenInput

Your ready handler does three things:

  1. Grabs the current value of #mktu_class and stores it in mktuClassID.
  2. Binds a change handler to #mktu_class which stores its value in mktuClassID.
  3. Calls tokenInput with a URL of "/items/" + mktuClassID where mktuClassID 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 in mktuClassID but nothing will look at mktuClassID so the effect is the same). Perhaps your indentation is off and you really want the /items call to be inside the change handler:

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"
        })

If that's the case, then do away with mktuClassID entirely and add a $('#mktu_class').change() call to initialize things:

jQuery(document).ready ->
    $('#mktu_class').change ->
        mktuClassID = $(this).val()
        $('#registration_application_items').tokenInput('/items/' + encodeURIComponent(mktuClassID), {
            crossDomain: false,
            prePopulate: $('#registration_application_items').data('pre'),
            preventDuplicates: true,
            theme: 'facebook'
        })
    $('#mktu_class').change();

And you really should be properly paranoid and encode mktuClassID when you put it in a URL so I added an encodeURIComponent call too.

If you only want to call tokenInput the first time that #mktu_class changes then bind the change handler with one:

jQuery(document).ready ->
    $('#mktu_class').one 'change', ->
        mktuClassID = $(this).val()
        $('#registration_application_items').tokenInput('/items/' + encodeURIComponent(mktuClassID), {
            crossDomain: false,
            prePopulate: $('#registration_application_items').data('pre'),
            preventDuplicates: true,
            theme: 'facebook'
        })

That version would call tokenInput only the first time #mktu_class changed.

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