Jquery 如何向输入中添加值,如果有值,则打开div

Jquery 如何向输入中添加值,如果有值,则打开div,jquery,Jquery,我似乎无法理解这段代码的错误: 它本质上是将checked的值添加到收音机输入上,如果收音机具有该值,则打开一个div $(document).on("click","input[name=newBillingAddress]:radio",function(){ $("input:radio[name=newBillingAddress]:checked").val('checked'); $("input:radio[name=newBillingAddress]:not(:checked)

我似乎无法理解这段代码的错误: 它本质上是将checked的值添加到收音机输入上,如果收音机具有该值,则打开一个div

$(document).on("click","input[name=newBillingAddress]:radio",function(){
$("input:radio[name=newBillingAddress]:checked").val('checked');
$("input:radio[name=newBillingAddress]:not(:checked)").val('unchecked');
}


if($('#existingBillingRadio').val('checked')){
    $('.toggleBlock').slideDown();
});

如果您在条件内进行分配,则必须执行以下操作:

if($('#existingBillingRadio').val() == 'checked')
虽然我将只检查已检查的属性,并删除以前的val分配:

if($('#existingBillingRadio')is(":checked") {
    $(this).parent().find('.toggleBlock').first().slideDown();
}

上述条件始终为true,并且在页面加载时只运行一次。

要检查if语句中的值,必须对其进行比较

$(document).on("click", "input[name=newBillingAddress]:radio", function(){

   $("input:radio[name=newBillingAddress]:checked").val('checked'); 
   $("input:radio[name=newBillingAddress]:not(:checked)").val('unchecked');

    // removed extra }

    if($('#existingBillingRadio').val() === 'checked'){
         $(this).parent().find('.toggleBlock').first().slideDown();
    }

});
现在还不清楚您真正想要什么,但是单选按钮有一个选中的属性,而不是一个值。所以你应该检查的是

$(document).on("click", "input[name=newBillingAddress]:radio", function(){

   // $("input:radio[name=newBillingAddress]:checked").prop('checked', true);
   // $("input:radio[name=newBillingAddress]:not(:checked)").prop('checked', false);

   // the above lines don't make sense in this case anymore.
   // removed extra }

    if($('#existingBillingRadio').is(':checked')){
         $(this).parent().find('.toggleBlock').first().slideDown();
    }

});

您应该使用“prop”方法而不是“val”来选中复选框@DhavalMarthak在本代码中无处不在,而不是在所有代码中无处不在。就在处理支票箱和收音机的时候。你有一个额外的},巴德。右击中间,在你的问题中更精确。你用这个值来打开一个div==,bro是什么意思。此外,这不是检查是否选择了无线电输入选项的方式。回顾过去,我本可以把问题说得更清楚。。这就是我想要的。非常感谢:
$(document).on("click", "input[name=newBillingAddress]:radio", function(){

   // $("input:radio[name=newBillingAddress]:checked").prop('checked', true);
   // $("input:radio[name=newBillingAddress]:not(:checked)").prop('checked', false);

   // the above lines don't make sense in this case anymore.
   // removed extra }

    if($('#existingBillingRadio').is(':checked')){
         $(this).parent().find('.toggleBlock').first().slideDown();
    }

});