我正在尝试创建一个“测验”页面,用户可以在其中猜测一篇文章是真的还是假的。我有来 self 的数据库的数据信息,我正在尝试将其与用户单击的按钮的数据信息进行比较,以检查它们是否正确。例如,如果文章的数据信息为真,并且用户单击数据信息为真的按钮,则警告“你是对的”或其他任何内容。有人请帮忙!!
function loadArticle(){
$.get("/api/article", function(response){
$(".header").html(response[0].Headline);
$("h1").attr("data-info", response[0].is_real);
$(".body" ).html('<a target="_blank" href="${response[0].web_address}"> ${response[0].web_address} </a>');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "row" id="answers">
<button data-info="false" type = "button" class = "fa-bounce fas fa-times col-xs-4 col-xs-offset-2" id="fake">
FAKE
</button>
<div class="col-xs-4"></div>
<button data-info="true" type = "button" class = "fa-bounce fas fa-check col-xs-4 col-xs-offset-2" id="real">
REAL
</button>
</div>
最佳答案
您可以将data-*
与jQuery 方法进行比较.data()
这是一个示例,当您从 api 请求中获取数据并在您单击“真实”按钮时比较其值:
<button id="real" data-info="true">
real
</button>
<button id="real" data-info="false">
fake
</button>
var dataToCompare;
function loadArticle(){
$.get("/api/article", function(response){
dataToCompare = response[0].is_real;
}
}
$('#real').click(function() {
var data = $(this).data('info');
if(data === dataToCompare) {
alert('yes');
}
})
关于javascript - 比较文章和按钮的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51210287/