问题描述
创建此函数的目的是根据单选框的值返回不为null的值来发送警报,而该值将返回返回为null的单选框.页面上其他地方的功能是隐藏这些广播出现的表格,这是使此任务最初超出我的能力的原因.如果此div没有隐藏,并且这些表不是动态生成的,则可以通过向收音机添加必需的属性来解决.只要显示div,这实际上就可以工作,但是在隐藏时会中断.从本质上来说,我的任务是要使这台收音机成为必需.
这是JavaScript
<script type="text/javascript">
$(function validate() {
$(document).submit(function() {
<%For z = 0 to TotalUnits - 1%>
if ($("input[name='checkradio<%=z%>']:checked").length == 0)
alert("Select Yes or No for Needs Repair checkbox <%=z%>");
return false;
}
<%Next%>
$("submitbutton").click(function() {
$("#formID").submit();
document.getElementsByName("checkradio").addEventListener('click',
validate);
});
});
});
</script>
这是HTML
<label id="checklabel" name="checklabel">The Vehicle Requires Repair</label>"
<label id="yesradio">
<input type="radio" ((name="checkradio" & z)) value="1" id="Radio1"> Yes</label>
<label id="noradio">
<input type="radio" ((name="checkradio" & z)) value="0" id="Radio0"> No</label>
这是隐藏div的脚本(我清理了concat.但是将肉留在了自己的标签中)
<script>
<%for z = 0 to TotalUnits - 1%>
$( document ).ready(function() {
$("div.section<%=z%>").hide();
});
<%next%>
</script>
<%response.write(TmpString)%>
这是我的提交按钮(从标签上划掉)
<input value="Submit" type="submit" id="submitbutton" name="submitbutton"
onsubmit="validate()">
仅当按下提交"按钮且单选框的值为空时,警报才会触发.
由于每个单选按钮都是控件,因此它是您自己的权限,因此您需要检查是否有链接在一起的控件(通过name
属性)被检查.
首先,getElementsByName()
返回一个数组(请注意Elements
上的s
),因此没有.value
供您检查.
(哦,请注意,拥有多个<label id="yesno">
是无效的,因为元素需要具有唯一的id
属性.在这种情况下,您最好最好完全删除id="yesno"
.)
但是通过jQuery做到这一点要容易得多...
<script type="text/javascript">
$(function() {
$(document).submit(function() {
<%For z = 0 to TotalUnits - 1%>
if ($("input[name='checkradio<%=z%>']:checked").length == 0)
alert("Select Yes or No for Needs Repair checkbox <%=z%>");
return false;
}
<%Next%>
$("submitbutton").click(function() {
$("#formDVIR").submit();
});
});
});
</script>
通过使用input[name='checkradio<%=z%>']:checked
的选择器,您要jQuery查找所有名称为checkradio1
(或任何z
是)的输入控件,并且仅查找那些名称为checked
的输入控件.如果结果jQuery对象的长度大于1,则您知道至少选择了一个
The function created to send an alert based on a radio box's value returning as null is firing and sending alert while radio box has a value that should not be null. Somewhere else on the page is a function that hides the tables these radios appear on, and is what initially made this task out of my reach. If this div didn't hide, and these tables weren't generated dynamically then it could have been solved by adding a required attribute to the radio. This actually works as long as the div is showing, however breaks when hidden. I've essentially been tasked to take the long way around making this radio required.
Here is the javascript
<script type="text/javascript">
$(function validate() {
$(document).submit(function() {
<%For z = 0 to TotalUnits - 1%>
if ($("input[name='checkradio<%=z%>']:checked").length == 0)
alert("Select Yes or No for Needs Repair checkbox <%=z%>");
return false;
}
<%Next%>
$("submitbutton").click(function() {
$("#formID").submit();
document.getElementsByName("checkradio").addEventListener('click',
validate);
});
});
});
</script>
Here is the HTML
<label id="checklabel" name="checklabel">The Vehicle Requires Repair</label>"
<label id="yesradio">
<input type="radio" ((name="checkradio" & z)) value="1" id="Radio1"> Yes</label>
<label id="noradio">
<input type="radio" ((name="checkradio" & z)) value="0" id="Radio0"> No</label>
Here is the script that hides the div (I cleaned the concat. but left the meat in its own tags)
<script>
<%for z = 0 to TotalUnits - 1%>
$( document ).ready(function() {
$("div.section<%=z%>").hide();
});
<%next%>
</script>
<%response.write(TmpString)%>
Here is my submit button (stripped off the label)
<input value="Submit" type="submit" id="submitbutton" name="submitbutton"
onsubmit="validate()">
The alert should only fire when the submit button is pressed and the value of the radio is null.
Because each radio button is a control it is own right, you need to check if any of the controls that are linked together (via the name
attribute) are checked.
Firstly, getElementsByName()
returns an array (notice the s
on Elements
), so there is no .value
for you to check.
(Oh, and be aware that having multiple <label id="yesno">
is invalid, as elements need to have a unique id
attribute. In this case you're probably best just removing the id="yesno"
completely.)
But it's a lot, lot easier to do this via jQuery...
<script type="text/javascript">
$(function() {
$(document).submit(function() {
<%For z = 0 to TotalUnits - 1%>
if ($("input[name='checkradio<%=z%>']:checked").length == 0)
alert("Select Yes or No for Needs Repair checkbox <%=z%>");
return false;
}
<%Next%>
$("submitbutton").click(function() {
$("#formDVIR").submit();
});
});
});
</script>
By using the selector of input[name='checkradio<%=z%>']:checked
you're asking jQuery to find all input controls with a name of checkradio1
(or whatever z
is) and only those which are checked
. If the length of the resultant jQuery object is more than 1 you know at least one is selected
这篇关于Javascript函数发送警报触发,无论返回值如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!