使用变量传递类名

我尝试搜索这个,但没有得到可行的解决方案,我是 Jquery 的新手,我正在创建多项选择测验,一旦用户选择一个答案,我就禁用一个问题的单选按钮,我给出相同的类名对于每个输入,当我在下面的代码中使用类名时,我得到了想要的结果。如果类名为 q1。

$(document).ready(function(){
           $(".q1 :input").prop("disabled", true);
          });

我想对多个问题执行此操作,因此当我尝试通过变量传递类名时。

var val1 = $("input[type='radio']:checked").attr('class');

我在变量 val1 中获取了类的名称,但不确定如何在 jquery 中使用它来使上述代码正常工作。有人可以帮助我吗?我在网上尝试了很多解决方案,但没有任何效果。


呼唤远方
浏览 44回答 1
1回答

幕布斯6054654

如果你想禁用所有带有类名的输入元素,val1你可以这样做:$("." + val1).prop("disabled", true);例子:$(document).ready(function() {&nbsp; $("input").on("click", function() {&nbsp; &nbsp; var val1 = $(this).attr('class');&nbsp; &nbsp; $("." + val1).prop("disabled", true);&nbsp; });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><input type="radio" class="q1"/>Answer 1<input type="radio" class="q1"/>Answer 2<input type="radio" class="q1"/>Answer 3<br><input type="radio" class="r1"/>Answer 1<input type="radio" class="r1"/>Answer 2<input type="radio" class="r1"/>Answer 3
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5