繁体   English   中英

使用Ajax刷新Javascript下拉框

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

我对ajax有一个疑问,我有一个类别下拉框和一些javascript,因此当用户选择一个类别时,这将自动将页面重定向到该类别,而无需单击提交按钮。 我的问题很简单,如何在此过程中添加ajax以停止页面加载,而仅将div与类别项一起加载?

我正在使用表达式引擎cms,我知道这没关系,但是标记在代码中。

这是我的代码:

<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>

使用JQuery的$.ajax函数根据ajax回调替换select中的<option>标记。 通过Ajax调用的脚本将简单地返回<option>标记,可以直接将其加载到<select>

看看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