如何将Ajax响应函数内部声明的复选框值填充到变量

时间:2018-09-02 08:15:54

标签: javascript jquery ajax

在此代码段中,我得到的结果是var url = window.location.href; // Url format : localhost/mysite/?page=home if (url.indexOf('=') == -1) { var path = 'home'; } else { var path = window.location.href.split("=").pop(); } var pageName = $('h2:first').text(); if(path == 'home') { var pageName = 'Home'; } else { var pageName = $('h2:first').text(); } $("title").append(" - " + pageName); ,但是如果您签入代码,我会要求它将选定的on存储为值,但是没有发生。

user_email
$("#goBtn").on("click",function(){

  //assume I have the response data as follows:
  var response = {
    data:[
        {
          user_name: "zyx",
          user_email: "zyx.com",
          user_contact: "5555",
          state: "tn",
          city: "chennai",
          user_license: ""
        },
        {
          user_name: "abx",
          user_email: "abx.com",
          user_contact: "45555",
          state: "tn",
          city: "mumbai",
          user_license: ""
        }
      ]
  };

  for(i=0; i<response.data.length; i++){
  
$("#table").append("<tr class='tr'> <td> <input type='checkbox', value: response.data[i].user_email>"+response.data[i].user_name+" </td> <td> "+response.data[i].user_contact+" </td> <td> "+response.data[i].user_license+" </td> <td> "+response.data[i].user_email+" </td> <td> "+response.data[i].state+" </td> <td> "+response.data[i].city);
  }
});

//to process selected users: 
	$('#clickme').on('click', function(){
		var selected = $('input:checked');
	  var emails = [];
	  selected.each(function(){
	  	//alert(element);
	  	emails.push($(this).val());
	  });
	  
	  $('#check-emails').html(emails);
	});

我想存储电子邮件ID,以便将来可以使用该变量创建另一个Ajax请求,该变量包含用户选择的那些电子邮件ID。

2 个答案:

答案 0 :(得分:0)

您的问题是,在表示response.data[i].user_email元素的字符串中连接tr的值的方式是错误的。

下面是一个片段,说明了如何在JavaScript中串联字符串

$("#goBtn").on("click",function(){

  //assume I have the response data as follows:
  var response = {
    data:[
        {
          user_name: "zyx",
          user_email: "zyx.com",
          user_contact: "5555",
          state: "tn",
          city: "chennai",
          user_license: ""
        },
        {
          user_name: "abx",
          user_email: "abx.com",
          user_contact: "45555",
          state: "tn",
          city: "mumbai",
          user_license: ""
        }
      ]
  };

  for(i=0; i<response.data.length; i++){
  
$("#table").append("<tr class='tr'> <td> <input type='checkbox' value='" + response.data[i].user_email + "'>"+response.data[i].user_name+" </td> <td> "+response.data[i].user_contact+" </td> <td> "+response.data[i].user_license+" </td> <td> "+response.data[i].user_email+" </td> <td> "+response.data[i].state+" </td> <td> "+response.data[i].city);
  }
});



//to process selected users: 
	$('#clickme').on('click', function(){
		var selected = $('input:checked');
	  var emails = [];
	  selected.each(function(){
	  	//alert(element);
	  	emails.push($(this).val());
	  });
	  
	  $('#check-emails').html(emails);
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="goBtn">Go</button>
<table id="table" border=1>
  <tr>
   <th> Name </th>
   <th> contact </th>
   <th> license </th>
   <th> email </th>
   <th> state </th>
   <th> city </th>
  </tr>
</table>
    <div id="check-emails">
      
    </div>
    <button id="clickme">
    Click me
    </button>
		

例如,您有一个名为str的变量,其中包含一个人的名字,并且您想将该名字放入字符串中,方法如下:

var str = 'John Doe';
console.log('My name is ' + str + ', I\'m happy to be in StackOverflow !'); // output: My name is John Doe, I'm happy to be in StackOverflow !

答案 1 :(得分:0)

$("#table").append("<tr class='tr'> <td> <input type='checkbox' value = '"+response.data[i].user_email+"'>"+response.data[i].user_name+" </td> <td> "+response.data[i].user_contact+" </td> <td> "+response.data[i].user_license+" </td> <td> "+response.data[i].user_email+" </td> <td> "+response.data[i].state+" </td> <td> "+response.data[i].city+"</td>");