javascript - Ajax 调用检查复选框

标签 javascript jquery ajax jsp checkbox

我有两个复选框。现在单击其中一个复选框,我想对 jsp 页面进行 ajax 调用。该 jsp 页面将显示一个包含从数据库中获取的数据的表。

现在,问题是我有两个复选框,例如:

<div class="search-inputs">

        <input class="searchName" type="text" placeholder="Search Here.."></input>

        <input class="searchType1" type="checkbox" name="emailNotification"><label class="searchtype1label">Email Notification</label></input>
        <input class="searchType2" type="checkbox" name="SharingNotification"><label class="searchtype2label">File Sharing Notification</label></input>


        <input class="searchDateFrom" type="text" placeholder="Search From"></input>
        <input class="searchDateTo" type="text" placeholder="Search To"></input>
        <input class="Datesubmit" type="button" value="Search"></input>
        <div id="container"></div>

怎么做?请帮忙

My Script part : 

$(document).ready(function () {

            $('.search-select').on('change', function(){
            $('.search-inputs').children().hide();
            var classn =$(this).find(":selected").val();
            //alert(classn);
            if(classn=='searchName')
                {
                    //alert("searchname");
                    $('.'+"searchName").show();
                }
                else if(classn=='searchType')
                {
                    //alert("searchType");
                    $('.'+"searchType1").show();
                    $('.'+"searchtype1label").show();
                    $('.'+"searchType2").show();
                    $('.'+"searchtype2label").show();
                }
                else if(classn=='searchDate'){
                    //alert("searchType");
                    $('.'+"searchDateFrom").show();
                    $('.'+"searchDateTo").show();
                    $('.'+"Datesubmit").show();
                }

            });

            $('#emailNotification').on('change',function() {
            //var checked = $(this).is(':checked');
            if(this.checked){

            //alert("in");
            $.ajax({
                type: "POST",
                url: "searchOnType.jsp",
                data: {mydata: "emailNotification"},
                success: function(data) {
                    alert('it worked');
                    $('#container').html(data);
                },
                 error: function() {
                    alert('it broke');
                },
                complete: function() {
                    alert('it completed');
                }
            });

            }
      });

      });

但是如何在同一个页面显示表格呢?

最佳答案

你必须用这个稍微改变你的 html--

为两个复选框提供相同的类名。

<input class="searchType" type="checkbox" name="emailNotification" id="emailNotification"><label class="searchtype1label">Email Notification</label></input>
 <input class="searchType" type="checkbox" name="SharingNotification" id="SharingNotification"><label class="searchtype2label">File Sharing Notification</label></input>

现在 jquery 部分略有变化--

$('.searchType').click(function() {
    alert($(this).attr('id'));  //-->this will alert id of checked checkbox.
       if(this.checked){
            $.ajax({
                type: "POST",
                url: 'searchOnType.jsp',
                data: $(this).attr('id'), //--> send id of checked checkbox on other page
                success: function(data) {
                    alert('it worked');
                    alert(data);
                    $('#container').html(data);
                },
                 error: function() {
                    alert('it broke');
                },
                complete: function() {
                    alert('it completed');
                }
            });

            }
      });

关于javascript - Ajax 调用检查复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23269413/

相关文章:

Javascript继承扩展方法技巧

javascript - ReactJS : How to check each of array index containing the value or not?

Javascript string.replace 正则表达式

javascript - 在 Ajax 中选择更改/提交

c# - 如何将 JavaScript 数组传递给 ASP.Net MVC Action 作为帖子的一部分

javascript - React Native 项目的增强现实 (AR)

javascript - 将 json 文件从 php 打印到 javascript

jquery - slidesjs jquery幻灯片轮播中slides_container上方的空间 - css

c# - 通过 Ajax 将复杂对象发布到 MVC

jQuery通过ajax从下拉框的onchange事件加载表单元素