如何在onclick函数jquery中传递这两个参数

时间:2016-12-31 03:09:14

标签: javascript jquery

这里我使用AJAX,onload这个ajax工作正常后我显示结果,在这个结果我有一个叫做contac的按钮,从这个按钮我静态传递了两个参数(2,5)。如果我想通过我的 id 名称,我试过我无法做到,

$(document).ready(function(){
$.ajax({
type:'GET',
url:"http://www.domain.com/",
data:data,
success: function(data) {
//console.log(data);
  var count = data.length;
 if(count > 0)
 {
	  var htmlString='';
	  $.each( data, function( key, value ) {
		var name = value.name;  
		var availableFrom = value.availableFrom;  
		var postedBy = value.postedBy; 
		if(postedBy == "ROLE_AGENT"){
			var	postedBy = "Agent";
		}else if(postedBy == "ROLE_OWNER"){
			var	postedBy = "Owner";
		}else{
			var postedBy = "Builder";
		}							
		var superBuiltupArea = value.superBuiltupArea;  
		var superBuiltupAreaUnit = value.superBuiltupAreaUnit; 
		//var sqFt = 	superBuiltupArea+' '+superBuiltupAreaUnit;
		var price = value.expectedPrice;
	  htmlString+='<div class="row" id="prptylstt"><div class="col-sm-4" style="padding-left:0px;padding-right:0px;"><a href="propertydetails.php?id='+value.id+'"><img src="pics/02.jpg"style="width:100%;height:170px;"/  > </a></div><div class="col-sm-8" style="padding-left:20px;"><h4  style="color:#000;padding-top:12px;">'+name+'</h4><div class="row"><div class="col-sm-3"><p class="parclr">Price</p><h4 class="colrh" ><i class="fa fa-inr" aria-hidden="true"></i> 22.70L</h4></div><div class="col-sm-2 divbrdr" ><p class="parclr">Sqft</p><h4 class="colrh" >'+superBuiltupArea+'</h4></div><div class="col-sm-4 divbrdr" ><p class="parclr">Avaliable From</p><h4 class="colrh" ></i>'+availableFrom+'</h4></div><div class="col-sm-3 divbrdr"> <p class="parclr">PostedBy</p><h4 class="colrh" >'+postedBy+'</h4></div></div><hr><div class="row" style="padding-top: 5px;"><div class="col-sm-3" ><a class="par" data-toggle="modal" data-target="#myModal"><i class="fa fa-search" aria-hidden="true"></i>>Quick View</a></div><div class="col-sm-3 " > <a class="par"><i class="fa fa-heart-o" aria-hidden="true"></i> Shortlist</a></div><div class="col-sm-3 " ></div><div class="col-sm-3 "><div class="contact" style="text-align:center;">';
	  
	 // htmlString+='<button class="btn btn-default" id="prlstbtn" style="" data-toggle="modal" data-target="#contact">Contact</button>';
	
	  htmlString+='<button class="btn btn-default" id="prlstbtn" onclick="myFunction(2,5)">Contact</button>';
	  
	  htmlString+='</div></div></div></div></div>';
	  
	});
	$('#prop_listing').empty().append(htmlString);
	$("#search_not_found").hide();
 }
 else{
	$("#search_not_found").show();
	$("#prop_listing").hide();
 }
},
error:function(exception){
console.log('Exeption:'+exception);
}
}); 
});

2 个答案:

答案 0 :(得分:0)

假设您有名为idname的变量,您可以执行类似

的操作
htmlString += '<button class="btn btn-default" id="prlstbtn" onclick="myFunction("+id+","+name+")">Contact</button>';

htmlString += `<button class="btn btn-default" id="prlstbtn" onclick="myFunction(${id},${name})">Contact</button>`;

答案 1 :(得分:0)

myFunction.bind(this, 2, 5)

怎么样?

类似

htmlString += <button class="btn btn-default" id="prlstbtn" onclick="myFunction.bind(this, 2, 5)()">Contact</button>;

例如: http://jsbin.com/supuye/edit?html,js,console,output