自动聚焦由jQuery生成的输入已停止工作

时间:2020-03-20 09:02:00

标签: javascript jquery

我有一个表,其中有一个充当按钮的img:当我单击它时,该行将被克隆并插入到表中。 我想确保在添加新行时它是焦点。 最初,此代码有效,创建的最后一行是焦点所在,但现在已停止工作。 在写问题之前,我尝试了各种解决方案,但是它们没有用。我试图将html autofocus属性添加到最后一行,并在创建另一行时将其从上一行中删除,但是我认为这不是有效的方法。 这是该行的html代码:

     <tr>
        <td  class="col-2 align-middle">
            <label for="Server" data-toggle="tooltip" data-placement="top" title="<?php echo $title_Server?>">Server</label>
        </td>
        <td class="col-8 align-middle">
            <input name="Server" class="form-control" type="text" disabled>
        </td>
        <td class="col-2 align-middle my-auto">
            <img class="aggiungiRiga" src="img/add.svg" />
            <img class="modificaRiga" src="img/pencil.svg" />
            <img class="cancellaRiga"  src="img/delete.svg" />
        </td>
    </tr>

这是jquery / js管理行克隆的代码。

$('.aggiungiRiga').on('click', function (){
    event.preventDefault();
    $(this).closest("tr").clone().insertAfter('form > table > tbody > tr:last').find('input').attr('autofocus');//.find('input').focus();

    $('form > table > tbody > tr:last td input').prop('disabled', false);
    $('form > table > tbody > tr:last td:last img:nth-child(1)').hide();//css("visibility", "hidden");
    $('form > table > tbody > tr:last td:last img:nth-child(2)').hide();//css("visibility", "hidden");
    $('form > table > tbody > tr:last td:last img:nth-child(3)').attr('class', 'eliminaRiga');
})

1 个答案:

答案 0 :(得分:2)

我认为如果禁用该元素,则无法将其聚焦,因此我建议您在js中尝试此代码

public async Task<ResponseHandler> ExecuteAsync(CheckCommand command)
{
    ResponseHandler resp = new ResponseHandler();
    try
    {
        var check = await _client.CheckAsync(command.id); // it fails here and doesnt even hit my catch method when it returns an error
        _httpContextAccessor.HttpContext.Items["CommandResult"] = check;
        resp.id = check.ID.ToString();
        resp.balance = decimal.ToDouble(check.AccountBalance);
        resp.currency = check.CurrencyCode;
        resp.errorcode = 0;
        resp.errorDescription = "null";

    }
    catch (exception ex)
    {
        resp.id = "";
        resp.balance = 0;
        resp.currency = "";
        resp.errorcode = 1;
        resp.errorDescription = ex.StackTrace.toString();

    }
    return resp;
}
相关问题