简体   繁体   English

使用Ajax刷新Javascript下拉框

[英]Using Ajax to refresh a Javascript drop down box

I have a question regarding ajax, I have a category drop down box and some javascript in place so that when the user selects a category, this automatically redirects the page to that category without having to click a submit button. 我对ajax有一个疑问,我有一个类别下拉框和一些javascript,因此当用户选择一个类别时,这将自动将页面重定向到该类别,而无需单击提交按钮。 My question is simple, how can I add ajax to this process to stop the page load and just load the div with the category items? 我的问题很简单,如何在此过程中添加ajax以停止页面加载,而仅将div与类别项一起加载?

I am using expression engine cms, which I know doesn't matter but the tags are in the code. 我正在使用表达式引擎cms,我知道这没关系,但是标记在代码中。

This is my code: 这是我的代码:

<script language="JavaScript" type="text/javascript">
    var theTarget = "_top";
    function goThere() {
        if (!document.theForm.theMenu.selectedIndex=="") {
            window.open(document.theForm.theMenu.options[document.theForm.theMenu.selectedIndex].value, theTarget,"");
        }
    }
</script>

<form name="theForm" action="">
    <select name="theMenu" size="1" onchange="goThere()">
        {exp:channel:categories channel="store" style="linear" category_group="1"}
            <option value="{path='store-directory'}">{category_name}</option>
        {/exp:channel:categories}
    </select>
</form>

Use JQuery's $.ajax function to replace the <option> tags within the select based on an ajax callback. 使用JQuery的$.ajax函数根据ajax回调替换select中的<option>标记。 The script being called via Ajax would simply return <option> markup ready to be loaded right into the <select> 通过Ajax调用的脚本将简单地返回<option>标记,可以直接将其加载到<select>

Take a look at the $.ajax method with JQuery. 看看JQuery的$ .ajax方法。

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

<script>function goThere(val)
{
    $.post(val, function(data) {
      $("#categoryDiv").html(data);
});
}</script>


<form name="theForm" action="">
<select name="theMenu" size="1" onchange="goThere(this.value)">
{exp:channel:categories channel="store" style="linear" category_group="1"}
<option value="{path='store-directory'}">{category_name}</option>
{/exp:channel:categories}
</select>
<div id="categoryDiv">
</div>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM