我必须根据小脚本中的jsp变量在表单中使用动态ID。如何使用jQuery的ID选择器选择正确的元素而不会出现错误?

这是我的代码:

<form name="intakeOnly" id="intakeOnly_<%=complaint%>" method="post" action="../Intake.do">
<table class="normal">
  <tr>
    <td><b>Clinician Signature: </b> <span id="cSigI_<%=complaint%>"><%=clinicianSig%>,<%=signedDate%></span></td>
  </tr>
  <tr>
    <td><input type="button" id="submitSig_<%=complaint%>" name="submitSig" value="Sign and Save" onClick="intakeSubmit();"></td>
  </tr>
</table>
</form>


Javascript:

function intakeSubmit() {
   var complaint = <%=complaint%>;
   var fname = new String("<%=userFirstName%>");
   var lname = new String("<%=userLastName%>");
   var date = new String("<%=DateUtils.getDate()%>");
   jQuery("#cSigI_"+complaint).append("<br>"+fname+" "+lname + ","+ date);
}

最佳答案

这行:

var complaint = <%=complaint%>;


应该

var complaint = "<%=complaint%>";


应该更改其后的行以匹配该语法,无需new String(str)

10-05 23:00