cuốn sách gpt4 ai đã làm

jQuery tách() không... chia tách?

In lại Tác giả: Walker 123 更新时间:2023-11-28 16:18:51 28 4
mua khóa gpt4 Nike

我的代码中某处有错误,但看不到我做错了什么。

我拥有的是 facebook 用户 ID 的隐藏输入,它是通过 jQuery UI 自动完成填充的:


然后,我有一个 jQuery 函数,当单击链接将其发布到 friend 的墙上时,该函数就会运行。



window.fbAsyncInit = function() {
FB.init({
appId : '12345678', // App ID
channelUrl : '/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});

// Additional initialization code here

FB.login(function(response)
{
if (response.authResponse)
{
$("#send-voucher").click(function() {

// Post message to friend's wall

var opts = {
message : 'This is the message',
name : 'This is the name',
link : 'http://www.opticalexpress.co.uk',
description : 'This is the description',
picture : '/voucher_valid.png'
};

var referees = $("#fbid").val();

// remove last comma and space from the end of the string
referees = $.trim(referees);
referees = referees.substring(0, referees.length - 1);

var referee = referees.split(',');
referee.each(function() {

FB.api('/'+ referee +'/feed', 'post', opts, function(response)
{
if (!response || response.error)
{
alert('Posting error occured');
}
khác
{
alert('Success - Post ID: ' + response.id);
$("#send-voucher").hide();
$("#voucher-sent").fadeIn('slow');
}
});
});
});
}
khác
{
alert('Not logged in');
}
}, { scope : 'publish_stream' });
};

当我点击发送优惠券时,错误来自上面的 khác 语句中的警报对话框:

发生发布错误

我添加了另一个警报,以了解为什么在循环打开后会发生这种情况:

$.each(referee, function() {
referee = $.trim(referee);
alert(referee); // new alert for debugging
....

此警报的输出让我感到惊讶,因为 referee 的值是 12345, 567890(与隐藏输入相同,但删除了尾随空格和最后一个逗号)。

因此,jQuery 似乎没有正确地分割它,但它运行循环的次数是正确的,因为警报框会弹出两次,告诉我裁判,然后告诉我错误。 (两个警报框都显示两次只是为了澄清)。如果这是巧合,我尝试向隐藏输入添加更多 id,并且可以确认警报框显示的次数与 id 的数量相同。

所以我想知道我做错了什么,因为循环运行了正确的次数,但逗号分隔的 id 似乎根本没有被分割。

câu trả lời hay nhất

referee.each(function() 正在名为 referee 的数组上调用,而不是 jQuery 对象。

Hãy thử sử dụng$.each(referee, function().

http://api.jquery.com/jQuery.each/

gia hạn

您还可以使用正则表达式查看 .spliting,看看这是否有任何区别:

var referee = referees.split(/\s*,\s*/);

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

关于javascript - jQuery split() 不是...... split ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10448710/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress