Php 使用jquery ajax在dropdownlist中显示单个值

Php 使用jquery ajax在dropdownlist中显示单个值,php,jquery,ajax,Php,Jquery,Ajax,我得到了一个dropdownlist,它使用jQueryAjax过滤结果。但我不知道如何将列表中的值分开 当我使用此代码时: $("#orderby").on('change', function() { $.post("ajax/prijslaaghoog.php", $("#orderby").val('lowhigh'), function(result){ $("#productviewajax").html(result); }); $.po

我得到了一个dropdownlist,它使用jQueryAjax过滤结果。但我不知道如何将列表中的值分开

当我使用此代码时:

$("#orderby").on('change', function() {

    $.post("ajax/prijslaaghoog.php", $("#orderby").val('lowhigh'), function(result){
        $("#productviewajax").html(result);
    });

    $.post("ajax/default.php", $("#orderby").val('default'), function(result){
        $("#productviewajax").html(result);
    });
});
当我选择一个值时,它运行两个php文件。如何使其在选择default时只运行default.php,在选择lowhigh时只运行prijslaaghoog.php


提前感谢

尝试将if子句放在$之前。如下所示:

$("#orderby").on('change', function() {
    if ($('#orderby').val() == 'lowhigh'){
        //post here.
    }else if ($('#orderby').val() == 'default'){
        //post here
    }
});
你可以这样做:

 $("#orderby").on('change', function() {
     if ($(this).val() == 'lowhigh'){
        $.post("ajax/prijslaaghoog.php", $("#orderby").val('lowhigh'), function(result){
            $("#productviewajax").html(result);
        });
     }else if ($(this).val() == 'default'){
       $.post("ajax/default.php", $("#orderby").val('default'), function(result){
            $("#productviewajax").html(result);
        });
    }
});
使用
$(“#orderby”).val('lowhigh')
可以设置dropdownlist的值。 相反,您应该过滤所选的选项:

$("#orderby").on('change', function() {

    var strUrl = "ajax/default.php";
    var strFilter = $('#orderby > option').filter(':selected').val();

    if (strFilter  == 'lowhigh'){
        strUrl = "ajax/prijslaaghoog.php";
    }

    $.post(strUrl, { filter: strFilter }, function(result){
        $("#productviewajax").html(result);
    });

});
而且,如果在每个选项中都有
数据
属性,则甚至不需要
if语句

<select id="orderby">
    <option value="default" data-post-url="default.php">Default</option>
    <option value="lowhigh" data-post-url="prijslaaghoog.php">lowhigh</option>
</select>

$("#orderby").on('change', function() {

    var option = $('#orderby > option').filter(':selected');

    $.post("ajax/" + option.data("post-url"), { 
        filter: option.val() 
    }, function(result){
        $("#productviewajax").html(result);
    });

});

违约
低高
$(“#orderby”)。在('change',function()上{
var option=$('#orderby>option')。过滤器(':selected');
$.post(“ajax/”+option.data(“post url”),{
过滤器:option.val()
},函数(结果){
$(“#productviewajax”).html(结果);
});
});

最后一个脚本中是否有打字错误?当我添加数据部分时,没有任何php脚本正在运行。我犯了一个错误,Sorri不是
$(“#orderby”)。filter
。。应该是
$('#orderby>option')。过滤器