如何在jquery ajax中实现if..else ..条件?

时间:2015-01-02 06:43:05

标签: jquery ajax

是否有可能在data: {name: $('select#name').val(),area: $('select#area').val(),property: $('select#property').val(),bed: $('select#bed').val(),possessions: $('select#possession').val(),ptype: $('select#ptype').val()},中使用... else ..语句。我能够每次发送单个值到下一页。所以请帮助我如何在下面的jquery部分中添加if else。在这里帮助将会提前感谢您。

<script src="jquery-1.10.2.js"></script>

<script type="text/javascript">
        jQuery(document).ready(function($) {
            $('.btnSearch').click(function(){
                makeAjaxRequest();
            });

            $('form').submit(function(e){
                e.preventDefault();
                makeAjaxRequest();
                return false;
            });

            function makeAjaxRequest() {
                $.ajax({
                    url: 'scr.php',
                    type: 'get',
                    data: {name: $('select#name').val(),area: $('select#area').val(),property: $('select#property').val(),bed: $('select#bed').val(),possessions: $('select#possession').val(),ptype: $('select#ptype').val()},
                    success: function(response) {
                        $('table#resultTable tbody').html(response);
                    }
                });
            }
        });
    </script>

我试过这么多,有人帮助我为什么if else条件不起作用:

<script type="text/javascript">
        jQuery(document).ready(function($) {
            $('.btnSearch').click(function(){
                makeAjaxRequest();
            });

            $('form').submit(function(e){
                e.preventDefault();
                makeAjaxRequest();
                return false;
            });

            function makeAjaxRequest() {
                var data;
                if ($('#name').val()!="") {
                    data = { name: $('select#name').val() };
                } else if ($('#area').val()!="") {
                    data = { area: $('select#area').val() };
                }
                else if($('#property').val()!="")
                {
                    data = {property: $('select#property').val()};
                }
                else if($('#bed').val()!="")
                {
                    data = {bed: $('select#bed').val()};
                }
                else if($('#possessions').val()!="")
                {
                    data = {possessions: $('select#possession').val()};
                }
                else if($('#ptype').val()!="")
                {
                    data = {ptype: $('select#ptype').val()};
                }

                $.ajax({
                    url: 'scr.php',
                    type: 'get',
                    data: data,
                    success: function(response) {
                        $('table#resultTable tbody').html(response);
                    }
                });
            }
        });
    </script>

3 个答案:

答案 0 :(得分:0)

它并不完全清楚你想做什么,但听起来你想根据某些条件只发送一个属性的数据。你需要在$.ajax()电话之外的某个地方这样做;你不能在对象文字中使用if...else

function makeAjaxRequest() {
    var data;
    if (some condition) {
        data = { name: $('select#name').val() };
    } else if (some other condition) {
        data = { area: $('select#area').val() };
    }
    // etc....

    $.ajax({
        url: 'scr.php',
        type: 'get',
        data: data,
        success: function(response) {
            $('table#resultTable tbody').html(response);
        }
    });
}

答案 1 :(得分:0)

如果您唯一的问题是每次选择任何下拉列表时都会发送一个值,那么这可能对您有所帮助......此解决方案将自动创建一个属性,该属性可以发送到ajax而不包含任何if else子句或任何条件

$("select").on("change", function (e) { // can specify the ids of the dropdown if you want some selected dropdowns to be in action
var currentSelection = $(this).val(); //Select value of the dropdown which is selected
currentId = $(this).attr("id");

//this will create your parameter dynamically.. it wont require and if else clause
var data = {};
data[currentId] = $(currentSelection).val();

//after ajax call clear the selection of all other dropdowns, so that every time only 1 dropdown is selected
$("select").not("#" + currentId + "").val(-1);

});

浏览链接.. FIDDLE 希望这会有所帮助:)

答案 2 :(得分:0)

我认为你需要这样的东西:

$.ajax({
type: "POST",
data: (x > y) ? dataX : dataY,
success: success,
});