小编dma*_*rra的帖子

jQuery如何在每个()中向前看?

我似乎无法为此找到明确的答案.考虑以下:

var dupe = false;
    $(".syndUID").sort(function(a,b) {
        return a - b;
    }).each(function(i, el) {
        alert($(this).val() + " - " + $(this).next().val());
        if($(this).val() == $(this).next().val()) {
                dupe = true;
                return;
        };
    });
Run Code Online (Sandbox Code Playgroud)

此代码尝试在类的一组输入中查找重复值syndUID.它们分散在一个表单中,因此不会在DOM中彼此相邻.

next().val()虽然总是未定义.我使用了错误的功能吗?我如何简单地窥视下一个元素?我可以访问索引,但我不知道如何使用它.

编辑:

在阅读了评论和答案后,我意识到jQuery中确实没有合适的迭代器,这对我来说似乎非常愚蠢,因为它提供了each().我还有上面代码的另一个错误.这是我使用的最终解决方案:

// duplicate check
    var dupe = false;
    var prevVal = undefined;
    $(".syndUID").sort(function(a,b) {
        return $(a).val() - $(b).val();
    }).each(function() {
        if($(this).val() == prevVal) {
            dupe = true;
            return false;
        }
        prevVal = $(this).val();
    });
Run Code Online (Sandbox Code Playgroud)

对于通过谷歌找到这个问题的人来说,其他人提供的答案可能是一个不错的替代解决方案,但是根据我的需要,我发现这个问题已经足够了.

javascript jquery

8
推荐指数
1
解决办法
230
查看次数

当更新表单上的复选框时,以连续形式访问oldValue进行控制会在beforeUpdate中生成错误3251

这是我在MS Access中看到的一个陌生问题.我以连续的形式提供以下代码:

Private Sub thisForm_BeforeUpdate(Cancel As Integer)
If Not Cancel Then
    Debug.Print "pre-logging data changes..."

    ' here we need to doublecheck to see if any values changed.
    ' we simply iterate through the whole list, re-setting oldValue
    ' and newValue.
    For Each control In thisForm.Section(acDetail).controls
        If control.ControlType = acTextBox Or _
           control.ControlType = acComboBox Or _
           control.ControlType = acListBox Or _
           control.ControlType = acOptionGroup Or _
           control.ControlType = acCheckBox Then
            Debug.Print control.Name
            oldValues(control.Name) = control.oldValue
            newValues(control.Name) = control.value
        End If …
Run Code Online (Sandbox Code Playgroud)

ms-access vba ms-access-2007 access-vba

6
推荐指数
1
解决办法
2010
查看次数

如何改进Thread的这个包装?

考虑以下抽象类:

public abstract class Worker {
    protected bool shutdown;
    protected Thread t;

    /// <summary>
    /// defines that we have an auto unpause scheduled
    /// </summary>
    private bool _unpauseScheduled;

    /// <summary>
    /// when paused; schedule an automatic unpause when we
    /// reach this datetime
    /// </summary>
    private DateTime pauseUntil;


    private bool _isStopped = true;
    public bool IsStopped {
        get {
            return t.ThreadState == ThreadState.Stopped;
        }
    }

    private bool _isPaused = false;
    public bool IsPaused {
        get {
            return _isPaused;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c# multithreading

0
推荐指数
1
解决办法
235
查看次数

删除键/ val哈希对会留下键.为什么?

请考虑以下代码:

$foo{'bar'}->{'lala'} = "lol";
delete($foo{'bar'}->{'lala'});
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我希望"lala"条目完全消失(键和值).但是,当我使用Dumper转储此哈希时,它看起来像这样:

$foo => {
    'bar' => {
        'lala' => {}
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么钥匙留在后面?我想删除值和键.以下是实际相关代码和结果.

用于删除的代码:

delete( $state->{calls}->{$call_id} );
Run Code Online (Sandbox Code Playgroud)

输出之前:

$VAR1 = bless( {
  'packet' => {},
  'calls' => {
    ' 1330718109168
' => {
      'eventcount' => 0,
      'caller_id_name' => ' 
',
  '    channels' => [
        ' 1330718109168
'
      ],
      'entered_time' => 1330718109,
      'caller_id_num' => ' 
'
    },
    ' 1330718097167
' => {
      'eventcount' => 277,
      'caller_id_name' => ' Cell Phone   NY
',
      'channels' => [
        ' …
Run Code Online (Sandbox Code Playgroud)

perl hash

-2
推荐指数
1
解决办法
245
查看次数

从散列中的数组中获取随机元素

我在google上看到了很多关于如何获得随机数组索引的结果,但是我无法将它应用于这种情况.

考虑以下:

my %hash;
my @array = {"foo", "bar", "poo"};

$hash->{mykey} = @array;
Run Code Online (Sandbox Code Playgroud)

如何从$ hash - > {mykey}中的数组中获取随机元素?类似下面的代码不起作用:

my $element = $hash->{mykey}[rand($hash->{mykey})];
Run Code Online (Sandbox Code Playgroud)

编辑:所以下面的答案非常有用.特别是我的问题更复杂的是我正在使用线程模块,完全忘了共享我附加到哈希元素的数组!因此,答案对我来说不起作用.

在确定了这种疏忽后,下面的解决方案完美无缺.

arrays random perl

-2
推荐指数
1
解决办法
2478
查看次数