繁体   English   中英

JavaScript / Ajax-使用多个变量发送数据和接收响应

[英]JavaScript/Ajax - Send Data and Receive response with multiple variables

这是我的JavaScript代码,用于发送后期操作:

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

<script type="text/javascript">
$(document).ready(function() {
    var ColorId = "1";
    $( "#targetButton" ).click(function() {
            $.ajax({
                url: 'checkcolors.php',
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    var arr = data.msg.split(',');
                    arr.forEach(function(id){
                        $('#' + id.trim()).hide();
                    });
                    //$('#target').html(data.msg);
                },
                data: ColorId
            });
    });     

}); 
</script>
<button type="button" id="targetButton">Send</button>
<div style="BlackAndWhite" id="24604682">24604682</div>
<div style="BlackAndWhite" id="24604682x">24604682x</div>
<div style="BlackAndWhite" id="24604679">24604679</div>
<div style="BlackAndWhite" id="24604621">24604621</div>

以下是checkcolors.php的结果:

24604603, 24604684, 24604640, 24604609, 24604682, 24604686, 24604681, 24604689, 24604602, 24604679, 24604680, 24604622, 24604685, 24604683, 24604621, 24604677, 24604688,

我也可以像这样打印它们:

24604603
24604684
24604640
24604609
24604682
24604686
24604681
24604689
24604602
24604679
24604680
24604622
24604685
24604683
24604621
24604677
24604688

我可以根据需要设置这些数字的格式。

我该怎么办!

我有具有返回结果的相同ids html div元素。 当这些数字返回时,我必须隐藏所有带有来自checkcolors.php响应中所示ID的div元素。

我怎样才能做到这一点 ?

提前致谢!

这是一种非常粗糙的方法:

    <script type="text/javascript">
    function send() {
        var person = {
            name: $("#id-name").val(),
            address:$("#id-address").val(),
            phone:$("#id-phone").val()
        }

        $('#target').html('sending..');

        $.ajax({
            url: 'checkcolors.php',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var arr = data.msg.split(',');
                arr.forEach(function(id){
                    $('#' + id.trim()).hide();
                });
                //$('#target').html(data.msg);
            },
            data: person
        });
    }
</script>

为什么style =“ BlackAndWhite”可以是class =“ BlackAndWhite” ???

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

<script type="text/javascript">
$(document).ready(function() {
    var ColorId = "1";
    $( "#targetButton" ).click(function() {
        $.ajax({
            url: 'checkcolors.php',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                console.log(data);
                var arr = data.split(',');
                var html = "";
                arr.forEach(function(id){
                    html += "<div style='BlackAndWhite' id='" + id + "'>" + id + "</div>";
                });
                $('#target').html(html);
            },
            data: ColorId
        });
    });     

}); 
</script>
<button type="button" id="targetButton">Send</button>
<div id="target"></div>

暂无
暂无

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

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