我是jquery的新手,但我想在我的项目中使用它.我正在尝试循环遍历#rate_box中的所有链接并向其添加点击事件.这个点击事件会将一些数据发布到一个外部的PHP脚本,然后它应解除所有链接上的点击事件(所以人们不能快速连续两次.)然后它应该将从PHP脚本收到的数据放入一个span标签名为#status.
但是我的代码甚至没有执行警报("索引:"+ i).我正确绑定了吗?
<script type="text/javascript">
$(document).ready(function(){
$('#rate_box a').each(function(i) {
$(this).click(function() {
alert("Index: "+i);
$.post("../includes/process/rating.php", {id: "<?php $game_id ?>", type: "game", rating: i+1},
function(data) {
$('#rate_box a').each(function(i) {
$(this).unbind('click');
}
$('#status').html(data).fadeIn("normal");
});
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud) 在我正确的Javascript中进行实验,我试图从另一个方法(WaitAndSayHello)运行一个方法(SayHello).由于涉及回调,我使用bind来确保每个方法中的'this'引用该对象.
pretendThingConstructor = function (greeting) {
this.greeting = greeting;
this.SayHello = function() {
console.log(this.greeting); // Works
};
this.WaitAndSayHello = function() {
setTimeout(function() {
console.log(this)
this.SayHello() // Fails
}, 500);
}
this.WaitAndSayHello.bind(this); // This bind() doesn't seem to work
}
var pretend_thing = new pretendThingConstructor('hello world');
pretend_thing.SayHello();
pretend_thing.WaitAndSayHello();
Run Code Online (Sandbox Code Playgroud)
代码打印'hello world',然后第二次'对象#没有方法'SayHello'失败.我可以从console.log中看到'this'指的是事件.但是不应该使用bind()修复此问题?
如何使bind()工作?
另外,我想干净利落地做到这一点:即,不要在多个地方引用对象的名称.
我是一个live()用户,我知道不知何故live()已被弃用.我试图使用bind()在新添加的dom上运行事件,但事实并非如此.
你能告诉我怎么做bind()吗?或者还有其他方法来完成这项任务吗?
这是html:
<table>
<tr>
<td> hi there <span class="click">click me</span></td>
<tr>
<tr>
<td> hi there2 <span class="click">click me</span></td>
<tr>
<tr class="end">
<td></td>
</tr>
</table>
<button class="add_new">add new row</button>
Run Code Online (Sandbox Code Playgroud)
这是js:
var new_row = '<tr><td> hi there, new row! <span class="click">click me</span></td><tr>';
$('.add_new').click(function() {
$('.end').before(new_row);
});
$('.click').bind('click', function() {
alert('clicked');
});
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我想使用本机jquery的方法而不是插件.这是我的jsfiddle http://jsfiddle.net/bondythegreat/z3uXH/
我有一个脚本,像这样:
$(".b").hide();
$(".uid").bind("change", function() {
if($(this).val().length>0) {
$(".b").show();
} else {
$(".b").hide();
}
});
Run Code Online (Sandbox Code Playgroud)
这可以按您期望的那样工作,但是要显示“ .b”,我必须将焦点放在“ .uid”上。有什么办法可以让我获得“。” 显示而不点击“ .uid”之外?这是一个提交按钮,在将某些内容输入到文本字段中之前,该按钮一直处于隐藏状态,并且我希望在将某些内容输入到字段中时立即显示该提交按钮,而无需单击该字段。
我有如下WPF xaml代码:
<StackPanel FocusManager.FocusedElement="{Binding FocusedElement}">
<TextBox Name="txtbox1" Text="FirstText"/>
<TextBox Name="txtbox3" Text="SecondText"/>
<TextBox Name="txtbox2" Text="ThirdText"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
如何在ViewModel中将FocusedElement绑定到属性?类似的代码如下:
Switch(Type)
{
Case "FirstType" :
FocusedElement = "txtbox1";
break;
Case "SecondType" :
FocusedElement = "txtbox2";
break;
Case "ThiredType" :
FocusedElement = "txtbox3";
break;
}
Run Code Online (Sandbox Code Playgroud) 我试图以这种方式使用旧的bind2nd函数:
template<typename T>
class printer
{
public:
void operator()(T a, string& kd)
{
cout<<a<<endl;
}
};
int main(int argc, char *argv[])
{
string nme = "J-dar";
auto f1 = bind2nd(printer<int>(),nme);
//f1(5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我收到很多错误:
required from here
error: no type named 'first_argument_type' in 'class printer<int>' class binder2nd ^
error: no type named 'second_argument_type' in 'class printer<int>' typename _Operation::second_argument_type value; ^
error: no type named 'second_argument_type' in 'class printer<int>' binder2nd(const _Operation& __x, ^
error: no type named 'result_type' in 'class …Run Code Online (Sandbox Code Playgroud) #include <functional>
#include <iostream>
using namespace std;
class test {
public:
test(){ p = new int[10];}
void print_info(double a)
{
cerr << a << endl;
}
~test(){
cerr << __LINE__ << endl;
delete []p;
}
private:
int *p;
};
int main()
{
test t;
std::function<void(void)> f = std::bind(&test::print_info, t, 2.0);
//std::function<void(void)> f = std::bind(&test::print_info, std::cref(t), 2.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它会崩溃,因为它会test::~test()被调用两次.但是,如果我t用std::cref(t)(或std::ref(t), &t)替换, ~test()则在退出main()时只调用一次.
我没弄清楚原因.我使用gcc 4.6.3在Ubuntu 12.04 64bit上.
我正在尝试在for循环中绑定click事件,但是任何删除按钮都只删除最后一个列表项.你知道为什么会这样吗?
for ( var key in editedPredef.predefinition) {
var listItem = $("<li></li>").addClass("ui-widget-content");
listItem.text("some text");
var removeListItem = $("<span class=\"ui-icon ui-icon-closethick\"></span>");
removeListItem.click(function() {
listItem.remove();
});
listItem.append(removeListItem);
containerElement.append(listItem);
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码,它将创建一个Java gRPC服务器并尝试将其绑定到USD / tmp / foo:
EpollEventLoopGroup group = new EpollEventLoopGroup();
Server server =
NettyServerBuilder.forAddress(new DomainSocketAddress("/tmp/foo"))
.channelType(EpollServerSocketChannel.class)
.bossEventLoopGroup(group)
.workerEventLoopGroup(group)
.addService(new Impl())
.build()
.start();
server.awaitTermination();
Run Code Online (Sandbox Code Playgroud)
但是,此操作失败bind(..) failed: Invalid argument:
Exception in thread "main" java.io.IOException: Failed to bind
at io.grpc.netty.NettyServer.start(NettyServer.java:231)
at io.grpc.internal.ServerImpl.start(ServerImpl.java:151)
at io.grpc.internal.ServerImpl.start(ServerImpl.java:75)
at com.google.devtools.javatools.jade.pkgloader.GrpcLocalServer.main(GrpcLocalServer.java:60)
Caused by: io.netty.channel.unix.Errors$NativeIoException: bind(..) failed: Invalid argument
at io.netty.channel.unix.Errors.newIOException(Errors.java:117)
at io.netty.channel.unix.Socket.bind(Socket.java:291)
at io.netty.channel.epoll.AbstractEpollChannel.doBind(AbstractEpollChannel.java:714)
at io.netty.channel.epoll.EpollServerSocketChannel.doBind(EpollServerSocketChannel.java:70)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1283)
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501)
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486)
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:989)
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254)
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:364)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)
at …Run Code Online (Sandbox Code Playgroud) 有三个文件。
context.js将导出的对象导出到bind:module.exports = {
exceptionValue: 99
};
Run Code Online (Sandbox Code Playgroud)
strategy.js 那就是导出我想调用绑定的函数: module.exports = events => {
if (this.exceptionValue !== 99) {
throw new Error(this);
}
return events;
};
Run Code Online (Sandbox Code Playgroud)
index.js 导入两个先前的文件:const context = require('./context');
const strategy = require('./strategy');
const strategyWithContext = strategy.bind(context);
return strategyWithContext(events);
Run Code Online (Sandbox Code Playgroud)
events是传递给的JSON对象的列表index.js。更准确地说,我正在从中导出此函数index.js,调用它的人将提供这些事件。但这没什么特别的,只是一个JSON对象列表。
问题在于,this策略函数内部的引用不起作用,并且始终会引发异常。我根本无法访问上下文对象。我究竟做错了什么?为什么不起作用?