【问题标题】:check for a value in all textboxes in a row检查一行中所有文本框中的值
【发布时间】:2012-10-15 19:26:04
【问题描述】:

当我检查是否有任何文本框在提供的行中有值或者它只检查第一个文本框时,我不断返回一个对象。如果任何文本框有值,我想将一行传递给 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;
}

【问题讨论】:

  • 代码的相关部分是什么/问题出在哪里?
  • 问题出在 var 块上。我希望它检查是否有任何文本框在 var 行中有值。我尝试了块 var 旁边的注释掉的代码以及底部 chkblock 和 chkblock2 的两个函数,但没有任何运气。

标签: jquery


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我最终只是用这个更新的函数在 td 中循环。感谢大家的意见。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 2010-12-06
      • 2014-01-01
      • 1970-01-01
      相关资源
      最近更新 更多