如何实现 if.else.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()},中的声明 .为此,我每次都可以将单个值发送到下一页,所以请帮助我如何在下面的jquery部分添加其他值。这里的帮助将不胜感激 提前谢谢。

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

目前还不完全清楚你想做什么,但听起来你想根据某种条件发送只有一个属性的数据。您需要在$.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);
        }
    });
}

如果您唯一的问题是每次选择任何下拉列表时都发送一个值,那么这可能会对您有所帮助......此解决方案将自动创建一个属性,该属性可以发送到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);

});

通过链接..小提琴希望这对:)有所帮助

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

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

最新更新