Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么被选中=";勾选“;不在js中验证?_Javascript_Html_Validation - Fatal编程技术网

Javascript 为什么被选中=";勾选“;不在js中验证?

Javascript 为什么被选中=";勾选“;不在js中验证?,javascript,html,validation,Javascript,Html,Validation,我对此有点困惑。我有这个html- <input type="radio" id="20577091" name="q_engine" value="20577091" style="position: absolute; opacity:0;" checked="checked" /> 这个javascript var chosen = ''; len = document.quote_form.q_engine.length; for (i = 0; i < len

我对此有点困惑。我有这个html-

<input type="radio" id="20577091" name="q_engine" value="20577091" style="position: absolute; opacity:0;" checked="checked" />

这个javascript

var chosen = '';
len = document.quote_form.q_engine.length;

for (i = 0; i < len; i++) {
   if (document.quote_form.q_engine[i].checked) {
        chosen = document.quote_form.q_engine[i].value
   }
}
var-selected='';
len=document.quote\u form.q\u engine.length;
对于(i=0;i
由于某些原因,它将无法验证。我选择了radio元素,但当选择submit(和alert var)时,它是空的

我有另一个带有多个单选按钮和标签的表单。如果单击标签,将选中单选按钮并进行验证

任何帮助都将不胜感激


谢谢。

这在Google Chrome中使用了相同的代码:

<html>
  <head>
</head>
<body>

<form name="quote_form">
<input type="radio" id="20577090" name="q_engine" value="20577090" style="position: absolute; opacity:0;" />
<input type="radio" id="20577091" name="q_engine" value="20577091" style="position: absolute; opacity:0;" checked="checked" />
</form>

<script>
var chosen = '';
len = document.quote_form.q_engine.length;

var chosen = '';
len = document.quote_form.q_engine.length;

for (i = 0; i < len; i++) {
   if (document.quote_form.q_engine[i].checked) {
        chosen = document.quote_form.q_engine[i].value
   }
}
alert(chosen);
</script>

</body>
</html>

选择的var=“”;
len=document.quote\u form.q\u engine.length;
选择的var=“”;
len=document.quote\u form.q\u engine.length;
对于(i=0;i

脚本放在表单之后。

因为单个单选按钮没有长度属性,所以需要添加条件使其工作

<html><body>

<form name="quote_form">
<!--Radio 1 : <input type="radio" id="20577090" name="q_engine" value="20577090" />-->
Radio 2 : <input type="radio" id="20577091" name="q_engine" value="20577091" checked="checked" />
</form>

<script type="text/javascript">
var chosen = '';

if (document.quote_form.q_engine.checked) {
    chosen = document.quote_form.q_engine.value
}
else {
    len = document.quote_form.q_engine.length;
    for (i = 0; i < len; i++) {
        if (document.quote_form.q_engine[i].checked) {
            chosen = document.quote_form.q_engine[i].value
        }
    }
}
alert(chosen);
</script>

</body>
</html>

第二台:
选择的var=“”;
if(文件、报价单、q引擎、已检查){
所选=document.quote\u form.q\u engine.value
}
否则{
len=document.quote\u form.q\u engine.length;
对于(i=0;i
无论您有一个单选按钮还是多个单选按钮,上述代码都将有效


您可以在此处阅读更多信息-

尝试正确格式化此帖子…所选的var=''和len=…都在单独的
文档行中。quote\u form.q\u engine
可能不是您期望的,例如,不是具有
length
属性的对象。定义“validate”。包括相关标记(至少应该包括一个提交按钮)和实际执行现在包含的代码段的JavaScript代码。您应该尝试提供一个实际产生问题的自包含文档,并清楚地说明问题是什么。“不会验证”这不是问题描述。您已将单选按钮的内联样式设置为
opacity=0
。是否在默认单选按钮上使用某种自定义图像?如果是,则可能是您选择的单选按钮顶部的图像没有正确指向默认单选按钮。我的一位同事告诉我o使用不透明度=0来解决IE问题。我们不希望单选按钮显示并将其完全定位在页面之外,但不透明度=0不起作用。jacouh,谢谢。如果您删除示例中的第一个单选按钮,您将看到我的问题,它会警告一个空值,而它应该警告选中的值单选按钮。我以为我没有失去理智。出于某种原因,如果只有一个单选元素,它将不起作用。我想我可以插入一个假的单选按钮来让它起作用。使用console.log()而不是alert():使用单选:document.quote_form.q_engine.value=20577091 len=undefined我是一个业余爱好者,不太能够解释最后的评论,但我假设控制台承认只有一个单选按钮它将无法按脚本工作。我添加了一个“虚拟”单选按钮到我的页面,它的工作。我正在处理的问题是加载时,如果只有一个选择,我希望它隐藏和选择,否则显示选择的单选元素需要选择一个。再次感谢。