【问题标题】:Can't detect select box name when POST-ingPOST 时无法检测到选择框名称
【发布时间】:2011-12-07 13:28:05
【问题描述】:

我有一个简单的选择框:

<select name="rims" id="rims">
  <option value="1">Silver</option>
  <option value="2">Golden</option>
  <option value="3">Platinum</option>
  <option value="4">Custom</option>
</select>

我使用 jQuery 来检测用户何时做出选择:

    $("#rims").change(function() {

        $("#new_div").load("./models/create-report.php?selected=rims", { value: $(this).val() }, function() {

        });
    });

问题:我在 $_POST 变量中唯一得到的是选定的值,但我不知道它是否来自“rims”选择框:

array
  'value' => string '2' (length=1)

因此,当我添加另一个选择框并且用户进行选择时,该值会覆盖上面的值。

问题:我怎样才能获得选择框的名称,以便 $_POST 可以保存来自多个选择框的选择?

【问题讨论】:

  • 如何进行 ajax 调用?
  • 我也很难关注你,你想什么时候访问 select 元素的 name 属性?
  • @Daniel A. White - 示例代码已更新。
  • @Scruffy The Janitor:我想从我的 .change 上加载的 php 脚本访问它
  • @Freelancer 因为它是从#rims onchange 事件中调用的,你不能假设它来自name='rims' 元素并将其作为值传递吗?

标签: jquery forms post http-post


【解决方案1】:

只需将 id/name 添加到帖子信息中即可。

$("#rims").change(function() {

    $("#new_div").load("./models/create-report.php?selected=rims",{ "value": $(this).val(), "id": $(this).attr('id'), "name": $(this).attr('name') }, function() {

    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2018-11-12
    相关资源
    最近更新 更多