我正在开发一个随机报价应用程序。点击新的报价按钮时会显示报价,但我想在页面加载时显示报价。我调用了一个函数,但它仍然无效。谢谢!
这是我的代码:
$(document).ready(function() {
function randomQuote() {
$('#get-quote').on('click', function(e){
e.preventDefault();
// Using jQuery
$.ajax( {
url: "http://quotes.stormconsultancy.co.uk/random.json",
dataType: "jsonp",
type: 'GET',
success: function(json) {
// do something with data
console.log(json);
data = json[0];
$('#quotation').html('"'+json.quote+'"');
$('#author').html('-- '+json.author+' --');
$('a.twitter-share-button').attr('data-text',json.quote);
},
});
});
$('#share-quote').on('click', function() {
var tweetQuote=$('#quotation').html();
var tweetAuthor=$('#author').html();
var url='https://twitter.com/intent/tweet?text=' + encodeURIComponent(tweetQuote+"\n"+tweetAuthor);
window.open(url)
});
}
randomQuote();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 0 :(得分:1)
尝试删除点击监听器。在randomeQuote()内部删除点击监听器。
将您的点击监听器置于Base.RowInserted.AddHandler<ItemLotSerial>((sender, e) =>
{
var serialrow = (ItemLotSerial)e.Row;
if (serialrow != null)
{
InfoINItemLotSerialExtNV serextrow = PXCache<INItemLotSerial>.GetExtension<InfoINItemLotSerialExtNV>(serialrow);
InventoryItem itm = PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(Base, serialrow.InventoryID);
if (itm != null)
{
InfoInventoryItemAttributeExtNV extrow = PXCache<InventoryItem>.GetExtension<InfoInventoryItemAttributeExtNV>(itm);
if (extrow != null)
{
serextrow.SearchType = extrow.SearchType;
serextrow.DiamondColor = extrow.DiamondColor;
}
}
}
});
document.ready
$(document).ready(function() {
randomQuote(); // call initially and get random quote
});
function randomQuote() {
$.ajax( {
url: "https://quotes.stormconsultancy.co.uk/random.json",
dataType: "jsonp",
type: 'GET',
success: function(json) {
// do something with data
data = json[0];
$('#quotation').html('"'+json.quote+'"');
$('#author').html('-- '+json.author+' --');
$('a.twitter-share-button').attr('data-text',json.quote);
},
});
$('#share-quote').on('click', function() {
var tweetQuote=$('#quotation').html();
var tweetAuthor=$('#author').html();
var url='https://twitter.com/intent/tweet?text=' + encodeURIComponent(tweetQuote+"\n"+tweetAuthor);
window.open(url)
});
}
$('#get-quote').on('click', function(e){
e.preventDefault();
randomQuote();
});
答案 1 :(得分:0)
删除onClick侦听器,以便在调用该函数时它将直接更新。
function randomQuote() {
$.ajax( {
...
success: function(json) {
... //do updates
},
});
}