Javascript jQuery if单选按钮名称和值的条件
我有一个单选按钮,像Javascript jQuery if单选按钮名称和值的条件,javascript,jquery,Javascript,Jquery,我有一个单选按钮,像 <input type="radio" name="user-type" value="Store"> <input type="radio" name="user-type" value="Brand"> 也试过了 if($("input[name=user-type]:checked").val()) == "Brand").click(function(){ $(".showstore").hide(); $(".sh
<input type="radio" name="user-type" value="Store">
<input type="radio" name="user-type" value="Brand">
也试过了
if($("input[name=user-type]:checked").val()) == "Brand").click(function(){
$(".showstore").hide();
$(".showbrand").show();
});
if ( $("input[name=user-type]:radio").val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
也试过了
if($("input[name=user-type]:checked").val()) == "Brand").click(function(){
$(".showstore").hide();
$(".showbrand").show();
});
if ( $("input[name=user-type]:radio").val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
这些都不起作用,任何更正都值得赞赏。谢谢
更新1
我试过用这种方法,这两种方法都隐藏了
if($("input[name=user-type]:checked").val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
else if($("input[name=user-type]:checked").val() == "Store"){
$(".showstore").show();
$(".showbrand").hide();
}
您有一个语法错误,错误地关闭了属于if语句的
括号
if($("input[name=user-type]:checked").val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
只是有一些语法错误:
$("input[name='user-type']:checked").each(function(){
if($(this).val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
});
试试这个
if(jQuery("input:radio[name='user-type']:checked").val()=="Brand"){
//do your stuff
}
您需要这样检查:
if($('input[name="user-type"]:checked').val() === "Brand")
{
alert("Test");
// do something here
}
尝试以下代码-它基本上会侦听单选按钮上的单击
,名称=用户类型
,并根据单击的单选按钮进行切换
$(function () {
$('.showstore').hide();
$('.showbrand').hide();
$("input[name=user-type]:radio").click(function () {
if ($('input[name=user-type]:checked').val() == "Brand") {
$('.showstore').hide();
$('.showbrand').show();
} else if ($('input[name=user-type]:checked').val() == "Store") {
$('.showstore').show();
$('.showbrand').hide();
}
});
});
工作小提琴:这很短
$('input:radio').change(
function(){
if($(this).val() == 'Store'){
$('.showbrand').hide();
$('.showstore').show();
}
else{
$('.showstore').hide();
$('.showbrand').show();
}
}
);
当然@karthikr有一个很好的功能性方法
这里是一个“是/否”单选按钮的示例,用于根据单选按钮选择是否禁用某个元素
$("input[name=button-name]:radio").click(function () {
if ($('input[name=input-radio-name]:checked').val() == "Yes") {
$( "element" ).removeClass('btn-display-none');
$( "element" ).prop('disabled', false);
} else {
$( "element" ).addClass('btn-display-none'));
$( "element" ).prop('disabled', true);
}
});
来源:用于自己的项目我尝试了这种方法,并在单击时将其隐藏起来。如果($(输入[名称=用户类型]:选中).val()=“品牌”){$(“.showstore”).hide();$(“.showbrand”).show();}否则如果($(输入[名称=用户类型]:选中).val()=“商店”){$(“.showstore”).show();$(.showbrand”).hide();}应该一直有效,每次更改选项时,它都应该显示/隐藏。@锁定您刚才接受的代码看起来像回答者的脸。。。哎呀。。不是吗?看看我的代码。。顺便说一句,您有权随时更改您的接受情况……:)谢谢你的回答-Karthik Malla