变化阿贾克斯后返回文本
我有一个循环:变化阿贾克斯后返回文本
do {
// show items
<input type="hidden" id="contestant_id" value="'.$row_contestants['contestant_id'].'">
<input type="hidden" id="user_id" value="'.$userinfo['user_id'].'">
<button class="vote" onclick="vote()">vote</button>
<div id="result"></div>
} while ($row....);
我的功能:
function vote() {
var val1 = $('#contestant_id').val();
var val2 = $('#user_id').val();
$.ajax({
url:"vote.php",
type: "POST",
data: { contestant_id: val1, user_id: val2 },
success: function(response) {
$('#result').html(response);
}
});
}
我想要做的是从“投”文本这里:
<button class="vote" onclick="vote()">vote</button>
更改为其他东西l ike“投票!”当我得到回应。我有点过,但当然它的显示位置:
<div id="result"></div>
而且这只是我的循环的第一个结果显示下。
任何想法?
谢谢。
您可以从HTML通过这个运作,并在脚本处理
<button class="vote" onclick="vote(this)">vote</button>
function vote (el) {
var val1 = $('#contestant_id').val();
var val2 = $('#user_id').val();
$.ajax({
url:"vote.php",
type: "POST",
data: { contestant_id: val1, user_id: val2 },
success: function(response) {
$('#result').html(response);
if (response == 'Yes') {
var text = el.text();
var values = text.split(' ');
var count = 1;
if (values.length == 2) {
count = values[1].parseInt + 1;
}
el.text('Voted ' + count);
}
}
});
}
这样做更有意义。但在我的vote.php文件中,我做了一个mysql插入,如果“是”,否则“否”。我还能如何发送成功响应? – Ironank
如果条件添加一个。更新了我的答案。但不知道你如何发送回应。 – vijaykumar
我改变了你的$('。vote')。html(response),因为它在.vote类中,而不是在#result中。它的工作原理,但如果我增加一个增量数字.vote? – Ironank
[ID必须是唯一的(http://*.com/questions/5611963/can-multiple-different-html-元素具有相同的id如果他们不同eleme),特别是因为它会导致[JavaScript]中的问题(https://developer.mozilla.org/en-US/docs/Web/HTML/ Global_attributes/id)和CSS,当你尝试与这些元素进行交互时。 –
为每个Id添加一些增量值,以使id独一无二,如id =“contestant_id_(某个唯一值)” –
我补充说,但现在我都搞砸了如何让它们变成变量。 – Ironank