javascript - 在 JQuery/Javascript 中未识别只读复选框已选中

标签 javascript jquery ruby-on-rails

我有带有编辑和保存选项的参数。例如下表

参数名称 Type1 Type2 Type3

测试rdb(checkBox) rdb(checkBox) rdb(checkBox) 编辑(按钮)

如果我单击编辑按钮,行将被启用,我需要选择任何一种类型复选框并保存类型。在保存类型时,我将复选框值分配为rdb='t'。这个't'将保存它在数据库中。现在我面临着这个问题。每当选择复选框时,值都会为空。下面是我正在使用的代码。

Mange_Page.html.erb

<script>
    $('.editoidbtn').click(function() {
        var $this = $(this);
        var oid = $this.closest('tr').find('td:first-child').filter(function() {
            return $(this).find('.editoidbtn').length === 0;
        });
        var oidName = $this.closest('tr').find('td:first-child').next().filter(function() {
            return $(this).find('.editoidbtn').length === 0;
        });
        var expected = $this.closest('tr').find('td:first-child').next().next().filter(function() {
            return $(this).find('.editoidbtn').length === 0;
        });

        if ($this.html() === 'Edit') {
            $this.html('Save');
            oid.attr('contenteditable', true);
            oidName.attr('contenteditable', true);
            expected.attr('contenteditable', true);
            $this.closest("tr").find("input[name='rd_b']").attr('disabled', false);
            $this.closest("tr").find("input[name='rd_v']").attr('disabled', false);
            $this.closest("tr").find("input[name='rd_c']").attr('disabled', false);
            $this.closest("tr").find("input[name='review']").attr('disabled', false);
            $this.closest('tr').css('border',  " solid skyblue");
        }
        else {

            if ($this.html() === 'Save') {
                saveRows($this);
                $this.html('Edit');
                oid.attr('contenteditable', false);
                oidName.attr('contenteditable', false);
                expected.attr('contenteditable', false);
                $this.closest("tr").find("input[name='rd_b']").attr('disabled', true);
                $this.closest("tr").find("input[name='rd_v']").attr('disabled', true);
                $this.closest("tr").find("input[name='rd_c']").attr('disabled', true);
                $this.closest("tr").find("input[name='review']").attr('disabled', true);
                $this.closest('tr').css('border', 'none');
            }
        }
    });

Java 脚本

function saveRows($this){

    tr181 = $('#TR_protocol').is(":visible");
    snmp = $('#SN_protocol').is(":visible");

    var values = [];
    var $row = $this.closest("tr"),
    $tds = $row.find("td");
  count = 0;
  $.each($tds, function() {
   values[count]=$(this).text();
    count++;
  });

  if(tr181){
   var parameter_name = values[0].trim();
   var expected_value = values[1].trim();
    if($row.find("input[name='rd_b']").attr('checked')){
      var rdb = 't'
     }else{
       rdb =null;
     }
    if($row.find("input[name=rd_v]").attr('checked')){
    var rdv = 't'
    }else{
     rdv =null;
    }
    if($row.find("input[name=rd_c]").attr('checked')){
      var rdc = 't'
    }else{
      rdc =null;
    }
    if($row.find("input[name=reviews]").attr('checked')){
        var review = null;
      }

    $.ajax({    
      type: "POST",
      contentType: "application/json",
      url: "/configuration/update_parameter",
      data: JSON.stringify({
        parameter_name: parameter_name,
        expected_value: expected_value,
        rdb: rdb,
        rdv: rdv,
        rdc: rdc,
        review: review,
       }),
        beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
        success: function (result) {
        if (result === 'false') {
            alert('Parameter not Updated  !!!');
            return false;
         } else {
            alert('Parameter Updated successfully  !!!');
          }
        }
     });
   }
 }
 }

从下面的部分我得到的 Ajax 响应为 nil。当我单击保存按钮时,即使我有雨雪复选框,控件也会转到其他部分。它必须与值“t”一起使用。

if($row.find("input[name='rd_b']").attr('checked')){
      var rdb = 't'
     }else{
       rdb =null;
     }

最佳答案

我认为问题在于您检查值​​是否检查的代码?

你尝试过使用吗,

example: .find("input[name=rd_v]").is(':checked') # returns true or false

关于javascript - 在 JQuery/Javascript 中未识别只读复选框已选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60274320/

相关文章:

php - 在 JQuery AJAX POST 上...请求失败 : URI too long (longer than 8190)

ruby-on-rails - Capistrano 无法创建符号链接(symbolic link) : file exist

ruby-on-rails - 构建新的 Rails 应用程序错误加载 'sqlite3' 没有明显的写入版本

Javascript 正则表达式 "Invalid group"?

javascript - 如何在 if 语句中链接 nullish 合并以确定不是未定义或 null

javascript - 如何向 React Material UI 扩展面板添加动态内容,同时一次只保留一个事件选项卡功能

ruby-on-rails - 如何更新模型属性的值

javascript - 如果对象未定义,加载组件的最佳方法

jQuery,单击时连续调用多个动画

Jquery Transversing - 在树上移动时遇到问题