【发布时间】:2019-07-29 17:37:41
【问题描述】:
我正在尝试根据单选按钮选择来输入所需的文本。输入文本 ID 是 Js 变量(连接到循环 var)。 我如何通过 jquery 访问它?
这是我的代码: JS:
var submyform = "<br> SubstantialOwner: <br> "
+"Personne physique <input type='radio' onclick='javascript:PPMCheck();' name='PPM[x"+$subforwhichaccount+$subcountForms+"]' id='PP[x"+$subforwhichaccount+$subcountForms+"]'> "
+"Personne Morale <input type='radio' onclick='javascript:PPMCheck();' name='PPM[x"+$subforwhichaccount+$subcountForms+"]' id='PM[x"+$subforwhichaccount+$subcountForms+"]' checked><br> "
+"Last name (obligatoire pou les PP) :"
+"<input type ='text' name='subln[x"+$subforwhichaccount+$subcountForms+"]' value='' id='ln[x"+$subforwhichaccount+$subcountForms+"]' >"
我的jquery:
<script>
$("#option1").click(function() {
$("#required_later").prop("required", false);
$("#required_later").prop("disabled", true);
});
$("#option2").click(function() {
$("#required_later").prop("required", true);
$("#required_later").prop("disabled", false);
$("#required_later").focus();
});
</script>
如何通过我的收音机 id 更改“#option1”:id='PP[x"+$subforwhichaccount+$subcountForms+"]'
【问题讨论】:
-
您确定
$subforwhichaccount+$subcountForms有效吗?你检查过你的元素吗?此外,$前缀变量的常见用法会告诉世界上的每个开发人员$something是一个 jQuery 元素。而在你的代码中(假设)那些不是。 -
#option1和#option2元素在哪里? -
@RokoC.Buljan 是的,它有效!上面的代码使用户可以根据需要创建尽可能多的表单(嵌套表单)。 #option1 相当于 id : 'PP[x"+$subforwhichaccount+$subcountForms+"]'。我不知道如何访问它,所以我称之为#option1
-
在无论如何禁用的元素上设置
required = false是没有意义的。你同意吗? -
你的
#required_later元素在哪里?