【问题标题】:how to redirect to next page if all check box is unchecked如果未选中所有复选框,如何重定向到下一页
【发布时间】:2014-02-12 14:03:17
【问题描述】:

我有 html 表格,请参阅下面的 html 表格,如果用户单击标题中的图像,它不会重定向它显示警报“请取消选中所有复选框”,如果用户取消选中所有复选框,它只会重定向.我的问题是它现在重定向,因为第一个 td 未选中,如果第一个 td 被选中,它会显示警报,它只接受第一个 td 我想获取所有 td。任何一个指南都可以。请参阅下面的代码

ajax

<script>

$(document).ready(function() {
    $("#redirect").click(function() {

        var chkBox=$("#checkAddress");

         if (chkBox.attr('checked') != 'checked')
        {

     var clientid=document.getElementById("client").value;

        $.blockUI(

       { 

         message: '<h2>Please wait...</h2><img src="/image/loader.gif" />',
     timeout: 2000

       }); 


    $.ajax({
            type:"post",

        data:"clientid="+clientid,
        success:function(data){

                         window.location = '?action=clientpricenotification&clientid='+clientid+'';
                          $("#result").html(data);
                         $('.blockUI').hide();
        }
    }); 
        }
        else
        {
            alert("Please uncheck all the td");
        }
    });
});

</script>

html

  <th style="text-align:center ;width:200px">Route Update</th>
    <th style="text-align:center ;width:90px">By(Emp)</th>
    <th style="text-align:center ; width:32px"> <img id='redirect'  src="/image/Picture12.png" style="margin:0px;cursor:pointer;"> </th>

 echo ' <td style="width:200px" id="CPH_GridView1_Status1'.$rows['id'].'" class="route status1 '.$rows["id"].' "><input type="checkbox" style="margin:0 0 0 93px;" id="checkAddress" '.$checked_value.' name="checkAddress" '.$checked_value.' ></td>       

 <td id="CPH_GridView1_user" style="width:90px" class="edit user '.$rows["id"].'">'.$rows["user"].'</td>    

<td style="width:65px" class=" '.$rows["id"].'"></td>';

【问题讨论】:

    标签: php jquery html ajax checkbox


    【解决方案1】:

    这将监听所有复选框上的change() 事件,因此当它发现 0 被选中时,它将重定向。

     var $checkbox = $("input[type=checkbox]");
     var url = "http://.....";
     $checkbox.change(function(){
       if( $checkbox.is(":checked").length == 0 ) { window.location=url; }
     });
    

    你应该知道如何将它应用到你的代码中就足够了。

    【讨论】:

      【解决方案2】:

      使用 document.getElementByTagname("input")..它将返回所有输入标签的列表,您必须遍历列表以检查每个复选框。现在您只检查第一个复选框...

          var allInputs = document.getElementsByTagName("input");
          for (var i = 0, max = allInputs.length; i < max; i++){
              if (allInputs[i].type === 'checkbox')
                  if (allInputs[i].attr('checked') != 'checked'){
                         //your logic
                      }
          }
      

      【讨论】:

        【解决方案3】:

        首先您应该将所有输入的 id 定义为:

        id="checkAddress_{anythingunique}"
        

        然后

        <script>
            var redir=1;
            $('input[id^="checkAddress_"]').each(function(){
                if(!$(this).attr('checked'))redire=0;
            });
            if(redir==1){
                //do your action
            }
        </script>
        

        【讨论】:

          猜你喜欢
          • 2014-02-08
          • 2018-11-07
          • 1970-01-01
          • 1970-01-01
          • 2010-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-26
          相关资源
          最近更新 更多