【问题标题】:Unable to retrieve the value checked button in radio button group无法检索单选按钮组中的值检查按钮
【发布时间】:2010-12-07 11:17:44
【问题描述】:

我正在尝试检索单选按钮组中选中按钮的值。

    <script type="text/javascript">

function ButtonClicked() {

    alert($('input[name=datetype] :checked').val());
    return;
}

</script>

<form action="test.php">

<input type="radio" value="d1" name="datetype" checked onclick="javascript: ButtonClicked()">Date 1 <br />
<input type="radio" value="d2" name="datetype" onclick="javascript: ButtonClicked()"> Date 2 <br />

</form>

输出始终为 'undefined' 。我是 jQuery(和 JS)的初学者,所以我可能遗漏了一些明显的东西,但查看大量示例并没有帮助。

【问题讨论】:

  • alert($('input[name=datetype]:checked').val());试试没有空格
  • @benhowdle89:您应该将其添加为答案。

标签: php jquery jquery-selectors


【解决方案1】:

alert($('input[name=datetype]:checked').val()); 不带空格试试

【讨论】:

  • 没问题,只要记得接受答案,如果它有效:)
【解决方案2】:

从 onclick 中删除“javascript”。

<input type="radio" value="d1" name="datetype" checked onclick="ButtonClicked()">Date 1 <br />
<input type="radio" value="d2" name="datetype" onclick="ButtonClicked()"> Date 2 <br />

【讨论】:

    【解决方案3】:

    更简单:

    <script type="text/javascript">
      function ButtonClicked(value) {
        alert(value);
        return;
     }
    
    </script>
    <form action="test.php">
    <input type="radio" value="d1" name="datetype" checked onclick="javascript:ButtonClicked(this.value)">Date 1 <br />
    <input type="radio" value="d2" name="datetype" onclick="javascript: ButtonClicked(this.value)"> Date 2 <br />
    </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-14
      • 2017-06-05
      • 1970-01-01
      • 2015-12-10
      • 2021-05-20
      • 2021-02-09
      • 2016-08-21
      • 2017-11-19
      相关资源
      最近更新 更多