【问题标题】:Get the value of Checkbox and hidden input element获取复选框和隐藏输入元素的值
【发布时间】:2011-06-20 14:13:48
【问题描述】:

html如下:

<table class="tablesorter">
 <g:each in="${vehicleInstanceList}" status="i" var="vehicleInstance">
 <tr class="${(i % 2) == 0 ? 'odd' : 'even'}" id ="rows">
    <td >
      <g:checkBox id = "isActive_${i}" class="isActive" name="isActive"  value="${vehicleInstance?.isActive}" />
      <input type="hidden" value="${vehicleInstance?.id}" name="vehicleId" id="isActive_${i}_" />
     </td>
</tr>

和 Jquery :

  $(".isActive").click(function() {
     var checkBox_id = $(this).attr("id");            // getting the id of checkbox
     var isActive =          // get the value of checkbox
     var  vehicleId  =      // get the value of hidden input element

  });

我必须获取复选框和隐藏输入元素的值我该怎么做? isActive 是一个布尔类型,我想用真/假来回答,如果检查为真,则为假

  var isActive = $('#'+checkBox).is(':checked');  i have tried to get value of checkbox but it always gives output as false

【问题讨论】:

    标签: jquery


    【解决方案1】:

    $("#elementID").val(); 将获取所选元素的值。对于复选框,您可以使用 $(this) 作为选择器,就像获取 id 一样。

    【讨论】:

    • $(this).val();给出输出,因为我尝试过类似这样的方法 var isActive = $('#'+checkBox).is(':checked'); // 总是假 var vehicleId = $('#'+checkBox+'_').val();// 给出正确的输出
    【解决方案2】:

    试试这个。

    var isActive =          $(this).is(":checked");
    var  vehicleId  =      $(this).closest("td").find(":hidden").val();
    

    【讨论】:

    • isActive 是一个布尔值,如果检查为真,则我希望输出真/假,否则为假
    • vehicleId 不在下一个 td 中,它在那个 td 中,所以最近的不会工作
    • 最近的会找到包含的 td 而不是下一个 td。
    【解决方案3】:

    请试试这个:

    1. 获取复选框的父级,该父级实际上也是隐藏的父级。
    2. 在父项中找到隐藏字段。

    简单...!

    样本sn-p

     $('.isActive').click(function(){
    
            var isActive = $(this).is(':checked');
    
            console.log(isActive);
    
            var _vehicle = $(this).parent().find('input[type=hidden]').val();
    
            console.log(_vehicle);
    
    
        });
    

    寻求帮助的小提琴链接:http://jsfiddle.net/baELR/2/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2011-05-15
      相关资源
      最近更新 更多