Jquery没有设置单选按钮

时间:2015-06-18 20:03:07

标签: javascript jquery asp.net

我正在尝试在弹出控件的底部设置“设置所有”单选按钮,这样当用户有很长的冲突列表要解决时,他们只需选择一个单选按钮,选项就会很快选择。但是,我的javascript触发,似乎找到单选按钮,但无法实际设置单选按钮。

我有一个gridview gvErrors,但是在每个gridview行的第二个单元格中都是一个带有选项(tblOptions)的表格。我尝试过使用.attr("checked", true).setAttribute("checked", true).prop("checked", true)。我在控制台中没有收到任何错误,但单选按钮都保持未选中状态。任何帮助都将不胜感激。以下是Javascript。

<script type="text/javascript" language="javascript">
    function selectAll(option) {
        var grid = document.getElementById("<%=gvErrors.ClientID%>");

        for (var i = 0; i < grid.rows.length; i++)
        {
            var row = grid.rows[i];
            var table =  $(row).find("tblOptions");
            var radio = $(table).find("input[name*=" + option + "]:radio");
            //$('td input:radiobutton', '#tblOptions').prop('checked', true);
            $(radio).prop("checked", "checked");
            var test = "";
        }
    };
</script>

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        //This handles the rows or colums selection          
        $("#<%=rdbCancelAll.ClientID%>").click(function() {
           selectAll("rdbCancel");
        });
    });
</script>

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        //This handles the rows or colums selection          
        $("#<%=rdbReplaceAll.ClientID%>").click(function() {
           selectAll("rdbReplace");
        });
    });
</script>

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        //This handles the rows or colums selection          
        $("#<%=rdbRenameAll.ClientID%>").click(function() {
           selectAll("rdbRename");
        });
    });
</script>

gridview的一个小例子:

<table class="tableinfo nocollapse c6" cellspacing="1" cellpadding="2" border="0" id="ctl00_Main_gvErrors">
<tbody>
  <tr class="tableinfobody tableinfoGray">
    <th scope="col"><span class="c1">Current Name</span></th>

    <th scope="col"><span class="c1">Options</span></th>

    <th scope="col">Differences</th>
  </tr>

  <tr class="tableinfobody">
    <td class="l"><span id="ctl00_Main_gvErrors_ctl02_lblName">Test1</span></td>

    <td class="l">
      <table id="ctl00_Main_gvErrors_ctl02_tblOptions" border="0">
        <tbody>
          <tr>
            <td><input id="ctl00_Main_gvErrors_ctl02_rdbCancel" type="radio" name=
            "ctl00$Main$gvErrors$ctl02$Options" value="rdbCancel" /><label for=
            "ctl00_Main_gvErrors_ctl02_rdbCancel">Cancel adding signal.</label></td>
          </tr>

          <tr>
            <td><input id="ctl00_Main_gvErrors_ctl02_rdbReplace" type="radio" name=
            "ctl00$Main$gvErrors$ctl02$Options" value="rdbReplace" /><label for=
            "ctl00_Main_gvErrors_ctl02_rdbReplace">Replace curent signal with
            imported signal.</label></td>
          </tr>

          <tr>
            <td><input id="ctl00_Main_gvErrors_ctl02_rdbRename" type="radio" name=
            "ctl00$Main$gvErrors$ctl02$Options" value="rdbRename" /><label for=
            "ctl00_Main_gvErrors_ctl02_rdbRename">Rename imported signal to:</label>
            <input name="ctl00$Main$gvErrors$ctl02$txtNewName" type="text" value=
            "Test1_1" id="ctl00_Main_gvErrors_ctl02_txtNewName" class="c2" /></td>
          </tr>
        </tbody>
      </table>
    </td>

    <td class="l">
      <input type="hidden" name="ctl00$Main$gvErrors$ctl02$hfParamInternalUnmatched"
      id="ctl00_Main_gvErrors_ctl02_hfParamInternalUnmatched" value=
      "EBC1-Test1" /> <input type="hidden" name=
      "ctl00$Main$gvErrors$ctl02$hfParamInternalMatched" id=
      "ctl00_Main_gvErrors_ctl02_hfParamInternalMatched" value="Test1" />

      <table class="tableinfo c5" cellspacing="1" cellpadding="2" border="0">
        <tbody>
          <tr class="tableinfobody tableinfoGray">
            <th>Value Name</th>

            <th>Current</th>

            <th>Imported</th>
          </tr>

          <tr class="tableinfobody">
            <td class="c3">Unit</td>

            <td class="c4"></td>

            <td class="c4">flag</td>
          </tr>
        </tbody>
      </table>
    </td>
  </tr>

  <tr class="tableinfobody tableinfoGray">
    <td class="l"><span id="ctl00_Main_gvErrors_ctl03_lblName">Test2</span></td>

    <td class="l">
      <table id="ctl00_Main_gvErrors_ctl03_tblOptions" border="0">
        <tbody>
          <tr>
            <td><input id="ctl00_Main_gvErrors_ctl03_rdbCancel" type="radio" name=
            "ctl00$Main$gvErrors$ctl03$Options" value="rdbCancel" /><label for=
            "ctl00_Main_gvErrors_ctl03_rdbCancel">Cancel adding signal.</label></td>
          </tr>

          <tr>
            <td><input id="ctl00_Main_gvErrors_ctl03_rdbReplace" type="radio" name=
            "ctl00$Main$gvErrors$ctl03$Options" value="rdbReplace" /><label for=
            "ctl00_Main_gvErrors_ctl03_rdbReplace">Replace curent signal with
            imported signal.</label></td>
          </tr>

          <tr>
            <td><input id="ctl00_Main_gvErrors_ctl03_rdbRename" type="radio" name=
            "ctl00$Main$gvErrors$ctl03$Options" value="rdbRename" /><label for=
            "ctl00_Main_gvErrors_ctl03_rdbRename">Rename imported signal to:</label>
            <input name="ctl00$Main$gvErrors$ctl03$txtNewName" type="text" value=
            "Test2_1" id="ctl00_Main_gvErrors_ctl03_txtNewName" class="c2" /></td>
          </tr>
        </tbody>
      </table>
    </td>

    <td class="l">
      <input type="hidden" name="ctl00$Main$gvErrors$ctl03$hfParamInternalUnmatched"
      id="ctl00_Main_gvErrors_ctl03_hfParamInternalUnmatched" value=
      "HCMData3-Testw" /> <input type="hidden" name=
      "ctl00$Main$gvErrors$ctl03$hfParamInternalMatched" id=
      "ctl00_Main_gvErrors_ctl03_hfParamInternalMatched" value=
      "PrimaryData3-Testw" />

      <table class="tableinfo c5" cellspacing="1" cellpadding="2" border="0">
        <tbody>
          <tr class="tableinfobody tableinfoGray">
            <th>Value Name</th>

            <th>Current</th>

            <th>Imported</th>
          </tr>

          <tr class="tableinfobody">
            <td class="c3">SA</td>

            <td class="c4">3, 239</td>

            <td class="c4">239</td>
          </tr>
        </tbody>
      </table>
    </td>
  </tr>

  <tr class="tableinfobody tableinfoBlue">
    <td>&nbsp;</td>

    <td>&nbsp;</td>

    <td>&nbsp;</td>
  </tr>
</tbody>

非常感谢任何帮助清除这一点。

1 个答案:

答案 0 :(得分:1)

将值用作选择器,如此

 function selectAll(option) {
            var radio = $("input[value=" + option + "]");
            $(radio).prop("checked", "checked");
        }

     $('input[type="button"]').on('click', function(){
         var value = $(this).data('attr');
            selectAll(value);
         });

http://jsfiddle.net/3u3z4bLn/3/