**** Scenario description: IN this way, I encountered a very common problem in the project, but it was not solved all the time. Finally, I solved it with the help of my friend. Therefore, I will simply describe the code and the process of solving it, so as to provide a reference for you when you encounter similar problems in the future.

Problem description: I need to do a generated verification code, and then the generated verification code into the corresponding box inside the function. But because the data is iterated,

So there’s a problem with setting the ID of text as a variable. The page looks like this:

\

The page code looks like this:

 <input type="text" readonly="true" style="width: 4rem;" value="" id="pantinecode"/>
  <button class="layui-btn layui-btn-mini links_edit" href="javascript:void(0);" οnclick="generatecode(${patient.id});" id="btn"><i class="layui-icon"></i>Generate captcha</button>
					
Copy the code

The ****js code looks like this:

function generatecode(id){
		$.ajax({
	  		 type : "post".url : "/sys/patientcode.htm"? patientId="+ id +".// This is my interface
	  		 data : {},
	  		 dataType : "json".async : false.success : function(data) {
	  		 if(data.code ! ='0') {$("#pantientcode").val(data.message);
	  		  else {
	  		  layer.alert("Build failed!"); }}}); }Copy the code

Ok, this is the normal way we all write it, but the result is:

\

Yes, I clicked the second button, and the generated verification code was displayed on the first one. It was a headache, so I asked my friend how to deal with this one.

**** directly to the code:

<input type="text" readonly="true" style="width: 4rem;" value="" id=${patient.id}/><! -- here is the variable, which IS the same as jquery -->
    <button class="layui-btn layui-btn-mini links_edit" href="javascript:void(0);" οnclick="generatecode(${patient.id});" id="btn"><i class="layui-icon"></i>Generate captcha</button>
					
Copy the code

\

Js looks like this:

function generatecode(id){
Copy the code
 $.ajax({
	  type : "post".url : "/sys/patientcode.htm"? patientId="+ id +".// This is my interface
	  data : {},
	  dataType : "json".async : false.success : function(data) {
	  if(data.code ! ='0') {$("#"+id).val(data.message);
	  else {
	  	layer.alert("Build failed!"); }}}); }Copy the code

\

And the result is this:

Perfectly adapted to.

Ok simple summary, this time there is such a problem, the reason is very simple, because I do not understand the operation of jquery, in fact, to an element

If you set an ID to double quotes, jquery will assume that your id is a fixed value

If the same element is assigned, the value after the id should be quoted as required. That’s one thing to keep in mind, and then when ajax is passing data,

When concatenating strings, be careful not to make a mistake, because if you make a mistake, the result will be a failure to assign values!

I have my own website to make machine clan, welcome everyone to visit!