检查一行中所有文本框中的值

时间:2012-10-14 18:39:37

标签: jquery

当我检查所提供的行中是否有任何文本框或者只检查第一个文本框时,我一直收到一个返回的对象。我想将一行传递给chkblock函数,如果任何文本框都有值,则返回true。

先谢谢你的帮助, 罗伊

你可以在这里看到整件事:http://jsbin.com/igaror/1/edit

    <html>
    <head>
    <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    <!--[if IE]>
      <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <style>
      article, aside, figure, footer, header, hgroup, 
      menu, nav, section { display: block; }
    </style>
    </head>
    <body>
      <table>
        <tr>
          <td><input type="checkbox" /><input id='1' class='name' type='hidden' value='test 1'/></td>
          <td><input type="text" class="room" value="44"/></td>
          <td><input type="text" class="room" /></td>
          <td><input type="text" class="room" /></td>
        </tr>
        <tr>
          <td><input type="checkbox" /><input id='1' class='name' type='hidden' value='test 2'/></td>
          <td><input type="text" class="room" /></td>
          <td><input type="text" class="room" /></td>
          <td><input type="text" class="room" /></td>
        </tr>
      </table>
    </body>
    </html>

   $('.room').change(function() {
     var row = $(this).closest("tr");
     var block = false; //row.find(":text.room").val() !== "";
     var vname = row.find(":input.name").val();
     block = chkblock2(row);
       alert("name: " + vname + " block:" + block);
     if(vname == "test 1"){
       $(this).closest("table").find(":input.name").each(function() {
         if ($(this).val() == "test 2"){
           var row = $(this).closest('tr');
           row.find(":text").attr("disabled",block);
           row.find(":text").val("");
              }
     });
             $(this).closest("table").find(":input.name").each(function() {
         if ($(this).val() == "test 1"){
           $(this).closest("tr").find(":text").attr("disabled",false);
           //$(this).val('');
              }
     });

     } else if(vname == "test 2"){

       $(this).closest("table").find(":input.name").each(function() {
         if ($(this).val() == "test 1"){
           var row = $(this).closest('tr');
           row.find(":text").attr("disabled",block);
           row.find(":text").val("");
              }
     });


        $(this).closest("table").find(":input.name").each(function() {
         if ($(this).val() == "test 2"){
           $(this).closest("tr").find(":text").attr("disabled",false);
           //$(this).val('');
              }

     });  

     }



   });

function chkblock(row){
  return row.find("input:text:not(:empty)");
}

function chkblock2(row){
  row.find(":input.room").each(function() {
    if ($(this).val() !== "" ){
      return true;
    }
    });
    return false;
}

2 个答案:

答案 0 :(得分:0)

我建议您使用数据绑定:使用JSON对象和knockoutjs获取输入值,而不是通过jquery过滤。

答案 1 :(得分:0)

我最后只是使用这个更新的函数循环执行td。感谢大家的投入。

function chkblock2($row){
  for (var i = 1; i < 4; ++i){
    if($row.find("td:eq(" + i + ") input.room").val() !== ""){
      return true;
    }
  }
    return false;
}