Jquery是否选中单选按钮
可能重复:Jquery是否选中单选按钮,jquery,html,radio-button,Jquery,Html,Radio Button,可能重复: 我现在有两个单选按钮,用户可以决定是否需要在价格中包含邮资: <input type="radio" id="postageyes" name="postage" value="Yes" /> Yes <input type="radio" id="postageno" name="postage" value="No" /> No 是 不 我需要使用Jquery来检查“是”单选按钮是否被选中,如果被选中,则执行一个append函数。有人能告诉我怎么做吗
我现在有两个单选按钮,用户可以决定是否需要在价格中包含邮资:
<input type="radio" id="postageyes" name="postage" value="Yes" /> Yes
<input type="radio" id="postageno" name="postage" value="No" /> No
是
不
我需要使用Jquery来检查“是”单选按钮是否被选中,如果被选中,则执行一个append函数。有人能告诉我怎么做吗
谢谢你的帮助
编辑:
我已将代码更新为此,但它不起作用。我做错什么了吗
<script type='text/javascript'>
// <![CDATA[
jQuery(document).ready(function(){
$('input:radio[name="postage"]').change(function(){
if($(this).val() == 'Yes'){
alert("test");
}
});
});
// ]]>
</script>
//
类似这样的内容:
if($('#postageyes').is(':checked')) {
// do stuff
}
试试这个
if($("input:radio[name=postage]").is(":checked")){
//Code to append goes here
}
或者,上面提到的——同样——使用稍微少一点多余的jQuery:
$('input:radio[name="postage"]').change(
function(){
if (this.checked && this.value == 'Yes') {
// note that, as per comments, the 'changed'
// <input> will *always* be checked, as the change
// event only fires on checking an <input>, not
// on un-checking it.
// append goes here
}
});
$('input:radio[name=“postage”])。更改(
函数(){
如果(this.checked&&this.value=='Yes'){
//请注意,根据评论,“已更改”
//将*始终*被选中,因为
//事件仅在检查时激发,而不是
//取消对它的检查。
//这里是什么
}
});
修订(改进了一些)jQuery:
//用文本“You're appendin'”定义一个div元素
//将该div分配给变量“added”
var追加=$('').text(“您追加了”!”;
//将“追加”的“id”指定给“追加”元素
added.id='added';
// 1. 选择“name”属性为“postage”的“”元素
// 2. 分配onChange/onChange事件处理程序
$('input:radio[name=“postage”]”)。更改(
函数(){
//检查单击的单选按钮是否为值为“是”的单选按钮
//元素的值是已检查的值(如注释中的@shef所示)
如果($(this.val()=='Yes'){
//将“追加的”元素追加到“body”标记
$(追加)。附于('正文');
}
否则{
//如果是,则“否”按钮将删除“附加”元素。
$(追加).remove();
}
});
var added=$('').text(“您正在追加”!”;
added.id='added';
$('input:radio[name=“postage”]”)。更改(函数(){
如果($(this.val()=='Yes'){
$(追加)。附于('正文');
}否则{
$(追加).remove();
}
});代码>
对
否
这将侦听单选按钮上的更改事件。当用户单击“是”时,事件将触发,您可以向DOM添加任何您喜欢的内容。尝试以下操作:
if ( jQuery('#postageyes').is(':checked') ){ ... }
这将侦听已更改的事件。我尝试过其他人的答案,但这些都不适合我,最后,这一个成功了
$('input:radio[name="postage"]').change(function(){
if($(this).is(":checked")){
alert("lksdahflk");
}
});
jQuery对象总是真实的。您可能希望使用$(…).length
。您的意思是#postageyes:checked
或$(“#postageyes”)。根据返回的布尔值,是(“:checked”)
?@pimvdb。因此调用.length()
是错误的。文档中写道:“与其他筛选方法不同,.is()不创建新的jQuery对象。相反,它允许您在不修改的情况下测试jQuery对象的内容。”--无需查看是否选中它。否则,它永远不会有价值。浪费资源@丹尼尔H:关于你的更新:!这很奇怪,只是我的网站不起作用。至少我知道代码是正确的,我会努力找出它的错误,谢谢所有的答案!对此不太确定,但是,$(“#postageyes:checked”
始终返回true?您是否必须使用.length
才能使其工作?已删除“或”之后的一切虽然这段代码片段可能会解决问题,但确实有助于提高您文章的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您代码建议的原因。明白了。下一个答案将有一个。
// defines a div element with the text "You're appendin'!"
// assigns that div to the variable 'appended'
var appended = $('<div />').text("You're appendin'!");
// assigns the 'id' of "appended" to the 'appended' element
appended.id = 'appended';
// 1. selects '<input type="radio" />' elements with the 'name' attribute of 'postage'
// 2. assigns the onChange/onchange event handler
$('input:radio[name="postage"]').change(
function(){
// checks that the clicked radio button is the one of value 'Yes'
// the value of the element is the one that's checked (as noted by @shef in comments)
if ($(this).val() == 'Yes') {
// appends the 'appended' element to the 'body' tag
$(appended).appendTo('body');
}
else {
// if it's the 'No' button removes the 'appended' element.
$(appended).remove();
}
});
$('input:radio[name="postage"]').change(function(){
if($(this).val() === 'Yes'){
// append stuff
}
});
if ( jQuery('#postageyes').is(':checked') ){ ... }
if($('#test2').is(':checked')) {
$(this).append('stuff');
}
jQuery('input[name="inputName"]:checked').val()
$('input:radio[name="postage"]').change(function(){
if($(this).is(":checked")){
alert("lksdahflk");
}
});