【问题标题】:regarding input name in javascript关于javascript中的输入名称
【发布时间】:2013-10-23 23:15:16
【问题描述】:

我正在尝试使用 jQuery mobile 和 javascript 制作一个可以在智能手机上使用的小测验。它还没有完全完成。

我正在尝试创建一个单选按钮列表,其中一个选择了值并提交,但是当这是选项之一时,为什么输入名称是“radio=choice-v-6”。

<script type="text/javascript">
var answer1 = 2;
function submit()
{ 
   if ($('input[name=radio-choice-v-6]:checked', '#myForm').val() == answer1)
   {
  alert("Correct");
  window.location = "#question-2"
   } 
   else 
   {
   alert("Incorrect");
   window.location = "#incorrect"
   }
}
</script>

然后当我使用 jQuery mobile 时我使用的预设按钮名称。

<form id="myForm" name="myForm">
      <fieldset data-role="controlgroup" id="radiobuttons" data-mini="true">
        <input name="radio-choice-v-6" id="radio-choice-v-6a" type="radio" onclick="question1()" value="1">
        <label for="radio-choice-v-6a">One</label>
        <input name="radio-choice-v-6" id="radio-choice-v-6b" type="radio" onclick="question1()" value="2">
        <label for="radio-choice-v-6b">Two</label>
        <input name="radio-choice-v-6" id="radio-choice-v-6c" type="radio" onclick="question1()" value="3">
        <label for="radio-choice-v-6c">Three</label>
    </fieldset>
  <td><button type="submit" id="submit" name="submit" value="Submit" onclick="submit()"></td>
  </form>

我假设我会将输入名称设置为“radiobuttons”或“myForm”

我是 JavaScript 新手,想知道为什么“radio-choice-v-6”有效。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    'input[name=radio-choice-v-6]:checked' 是选择器,仅针对已选中且名称为 radio-choice-v-6 的单选按钮。一般形式不能视为checked,字段集也不能。所以myFormradiobuttons 不会针对当前选中的单选按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-29
      • 2012-04-14
      • 2022-12-11
      • 2017-05-21
      • 2015-07-24
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      相关资源
      最近更新 更多