单选按钮名称和值的if条件
jQuery if condition for radio button name and value
我有一个单选按钮,像
<input type="radio" name="user-type" value="Store">
<input type="radio" name="user-type" value="Brand">
我试着写jQuery脚本像
if($("input[name=user-type]:checked").val()) == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
和also tried
if($("input[name=user-type]:checked").val()) == "Brand").click(function(){
$(".showstore").hide();
$(".showbrand").show();
});
和also tried
if ( $("input[name=user-type]:radio").val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
这些都不起作用,任何纠正都是赞赏的。谢谢。
Update1
I tried in this way and this hide both
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();
}
试试下面的代码-它基本上监听name=user-type
上的click
,并根据单击哪个单选按钮进行切换。
$(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();
}
});
});
一个工作小提琴:http://jsfiddle.net/FpUSH/1/
这是简短的
$('input:radio').change(
function(){
if($(this).val() == 'Store'){
$('.showbrand').hide();
$('.showstore').show();
}
else{
$('.showstore').hide();
$('.showbrand').show();
}
}
);
有一些语法错误:
$("input[name='user-type']:checked").each(function(){
if($(this).val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
});
您需要这样检查:
if($('input[name="user-type"]:checked').val() === "Brand")
{
alert("Test");
// do something here
}
工作小提琴示例
您有一个语法错误,您错误地关闭了parenthesis
属于if语句
if($("input[name=user-type]:checked").val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
演示$("input[name=user-type]").change(function(){
$(".showstore").toggle(this.value === "Store");
$(".showbrand").toggle(this.value === "Brand");
});
<<h2>新演示/h2> demo try this,
if(jQuery("input:radio[name='user-type']:checked").val()=="Brand"){
//do your stuff
}
显然@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);
}
});
来源:用于自己的项目
相关文章:
- 我已经创建了一个jquery转盘,并使用if条件来运行和停止转盘
- 带有 AJAX 响应或 AJAX 响应的 IF 条件.响应.Text 不起作用
- if条件中的递归混淆,JS如何解释“;真/假”;条件
- 如何在ejs模板中使用if条件
- for循环与if条件
- 中继器控件内的 IF 条件
- 比较IF条件下的两个javascript数值
- 如何在 javascript 中使用 document.getelementbyname 中的 if 条件
- 是否可以将鼠标悬停(evt)与if条件结合使用
- 当if条件被添加到循环中时,Javascript函数将停止工作
- 我的if条件的执行
- 为什么它没有进入 if 条件
- else-if条件在js渲染模板中
- 在模态中使用 e.target 的复杂 if 条件.js
- 达到IF条件后停止功能
- 如何使用流星模板上的if条件
- javascript setinterval使用if条件显示图像
- Check IF条件返回false,但IF中的语句仍然可以在JavaScript中工作
- if条件如何在javascript中计算其值
- for循环中嵌套if条件的setTimeout