【问题标题】:Auto Load Second Dropdown using AJAX使用 AJAX 自动加载第二个下拉列表
【发布时间】:2014-01-29 05:36:24
【问题描述】:

我在加载AJAX 时遇到问题,我试图通过Praveen Kumar 来关注这个答案 First drop down menu to auto change the options of a second dropdown 并从http://codex.wordpress.org/AJAX_in_Plugins 阅读有关 ajax 的信息,并在此处引用

从2.8版本开始,可以使用javascript全局变量ajaxurl 如果您想将 javascript 代码从 php 文件中分离到 仅 javascript 文件。这只适用于管理方面。

所以我想在我的页面中加载 ajax 没有问题。我的代码是这样的

HTML:

  <form action="#" method="POST">
    <select name="region" onchange="messi_code(this.value)">
        <option>Region Select</option>
        <option value="East">East</option>
        <option value="West">West</option>
        <option value="North">North</option>
            <option value="South">South</option>
    </select>
    <br>
    <select id="region_branch" name="region_branch">
        <option>Select City</option>
    </select>

</form>

AJAX:

<script type="text/javascript">
function  messi_code(parent){
     url= 'process.php?parent=' + parent,
     $.get(url,function(data){
       alert(data);
     /* $("#region_branch").html(data);*/
    });
}
</script>

顺便说一句,在 ajax 脚本中,我尝试了 POSTGET 并删除了 type,但 J-console 中仍然出现错误,错误显示为 Uncaught ReferenceError: ajaxfunction is not defined: onchange

希望能得到一些帮助。

【问题讨论】:

  • 我猜这可能是一个范围界定问题。如果你在函数声明之前添加这一行会发生什么? $(function(){ $('select[name="region"]').on('change', function(){ajaxfunction($('select[name="region"]').val()) }) };)
  • 你怎么称呼这个ajaxfunction() ?您还需要在..parent=' + parent,..$("#region_branch").html(data); },..type:'POST', 处添加逗号而不是分号
  • @user007 我在父下拉菜单中调用ajaxfunction() 并在onchange event 上触发它。正如你所说,我将分号更改为逗号,但我遇到了一个错误,即cannot call method ajax or undefined.
  • 尝试将您的 &lt;script&gt; 更改为 &lt;script type="text/javascript"&gt;
  • @joe42 它说的是未定义的函数。

标签: javascript php ajax wordpress


【解决方案1】:
<form action="#" method="POST">
    <select name="region" onchange="messi_fan(this.value);">
        <option>Region Select</option>

        <option value="East">East</option>
        <option value="West">West</option>
        <option value="North">North</option>
            <option value="South">South</option>
    </select>
    <br>
    <select id="region_branch" name="region_branch">
        <option>Select City</option>
    </select>

</form>


<script>
     function  messi_fan(parent){
     url= 'process.php?parent=' + parent;
     $.post(url,function(data){
       alert(data);
    });
}
</script>

jsfiddle

【讨论】:

  • 我有点例外,它说cannot call method POST,如果它在GET,那么cannot call method GET。我尝试了你提供的 jfiddle,它在那里工作。
  • 请尝试更改 jquery 版本。我在小提琴中使用 jquery 1.7
  • 对不起,我试图将此代码包含在 1.10.2 上运行的 wordpress 中,我仍在尝试使用其他方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-18
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 2017-03-08
相关资源
最近更新 更多