ajax在url中发送一个值

时间:2016-01-08 10:55:12

标签: javascript php jquery ajax

我遇到了在数据中发送另一个值的问题

data:'id='+id+'&action='+action+'&a=+abc',
像这样。 这里 abc是一个隐藏输入类型的php变量,可以在add-like-tj.php上发送

请尝试修复此代码。我不太了解ajax我是新来的。



function addLikes(id,action) {
	$('.demo-table #tutorial-'+id+' li').each(function(index) {
		$(this).addClass('selected');
		$('#tutorial-'+id+' #rating').val((index+1));
		if(index == $('.demo-table #tutorial-'+id+' li').index(obj)) {
			return false;	
		}
	});
	$.ajax({
	url: "add-like-tj.php",
	data:'id='+id+'&action='+action,
	type: "POST",
	beforeSend: function(){
		$('#tutorial-'+id+' .btn-likes').html("<img src='loaderIcon.gif' />");
	},
	success: function(data){
	var likes = parseInt($('#likes-'+id).val());
	switch(action) {
		case "like":
		$('#tutorial-'+id+' .btn-likes').html('<input type="button" title="Unlike" class="unlike" onClick="addLikes('+id+',\'unlike\')" />');
		likes = likes+1;
		break;
		case "unlike":
		$('#tutorial-'+id+' .btn-likes').html('<input type="button" title="Like" class="like"  onClick="addLikes('+id+',\'like\')" />')
		likes = likes-1;
		break;
	}
	$('#likes-'+id).val(likes);
	if(likes>0) {
		$('#tutorial-'+id+' .label-likes').html(likes+" Like(s)");
	} else {
		$('#tutorial-'+id+' .label-likes').html('');
	}
	}
	});
}
&#13;
.demo-table .highlight, .demo-table .selected {color:#F4B30A;text-shadow: 0 0 1px #F48F0A;}
.btn-likes {float:left; padding: 0px 5px;cursor:pointer;}
.btn-likes input[type="button"]{width:20px;height:20px;border:0;cursor:pointer;}
.label-likes {font-size:12px;color:#2F529B;height:20px;}
.desc {clear:both;color:#999;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="tutorial-63">
<input type="hidden" id="likes-63" value="9">
<div class="btn-likes">
<input type="button" title="Like" class="like" onclick="addLikes(63,'like')"></div>
<div class="label-likes">9 Like(s)</div>
</div>
&#13;
&#13;
&#13;

这是我正在使用的代码。

5 个答案:

答案 0 :(得分:2)

创建键/值对的对象......

data:{id:id, action:action},

答案 1 :(得分:2)

您可以尝试使用以下内容。

data:{id:id, action: action, a: 'abc'}

答案 2 :(得分:1)

像这样使用   data:{field1:variable1,field2:variable2},

答案 3 :(得分:1)

将您的功能调整为

    function addLikes(id,action) {
    $('.demo-table #tutorial-'+id+' li').each(function(index) {
        $(this).addClass('selected');
        $('#tutorial-'+id+' #rating').val((index+1));
        if(index == $('.demo-table #tutorial-'+id+' li').index(obj)) {
            return false;   
        }
    });
    $.ajax({
    url: "add-like-tj.php",
    data:{id:id, action: action, a: 'abc'},
    type: "POST",
    beforeSend: function(){
        $('#tutorial-'+id+' .btn-likes').html("<img src='loaderIcon.gif' />");
    },
    success: function(data){
    var likes = parseInt($('#likes-'+id).val());
    switch(action) {
        case "like":
        $('#tutorial-'+id+' .btn-likes').html('<input type="button" title="Unlike" class="unlike" onClick="addLikes('+id+',\'unlike\')" />');
        likes = likes+1;
        break;
        case "unlike":
        $('#tutorial-'+id+' .btn-likes').html('<input type="button" title="Like" class="like"  onClick="addLikes('+id+',\'like\')" />')
        likes = likes-1;
        break;
    }
    $('#likes-'+id).val(likes);
    if(likes>0) {
        $('#tutorial-'+id+' .label-likes').html(likes+" Like(s)");
    } else {
        $('#tutorial-'+id+' .label-likes').html('');
    }
    }
    });
}

然后在您的php文件(add-like-tj.php)中,您可以使用POST METHOD接收数据,例如

$id = $_POST['id'];
$action = $_POST['action'];
$a = $_POST['abc'];

答案 4 :(得分:0)

试试这个 -

<button class="button button-positive button-block button-outline" ng-click="triggerViewUpdate()">Trigger View update</button>