【问题标题】:open textfield after click on span does not work单击跨度后打开文本字段不起作用
【发布时间】:2015-03-09 07:16:38
【问题描述】:

我无法使用 JavaScript/jQuery 创建就地编辑功能。我不能使用插件editinplace,因为我需要在上面放一个日期选择器。我下面的代码由于某种奇怪的原因不起作用:

function editImpfDatumImpfung1Clicked(sender)
{
    var sText = $(this).text();
    var sId = $(this).attr("id");
    $(this).attr("id", "tobechanged");
    $(this).after('<input type="text" id="' + sId + '" value="' + sText + '" />');
    $(this).remove();
    bImpfung1Clicked = true;
    $("#" + sId).focus();
    $("#" + sId).datepicker(datepickerOptions);

    if (bFirstRegisterImpfung1 === true)
    {
        firstRegisterImpfung1();
        bFirstRegisterImpfung1 = false;
    }
}

$(document).ready(function()
{
    $elem = $("span[id^='impfung_1['");

    if ($elem.length > 0)
    {
        bFirstRegisterImpfung1 = true;
        $elem.click(editImpfDatumImpfung1Clicked);
    }
});

编辑:

控制台没有错误。

console.log($("#" + sId).val()); 导致undefined

编辑 2: 只需要像这样转义 id:

sId = sId.replace(/[[]/g,'\\[');
sId = "#" + sId.replace(/]/g,'\\]');

感谢您的建议。

【问题讨论】:

  • console 上的任何错误
  • 提供错误将有助于解决您的问题。
  • 你能提供一个jsFiddle吗?
  • 什么是相关/有代表性的 HTML?

标签: javascript jquery edit-in-place


【解决方案1】:

我想您在以下行中有错字:

$elem = $("span[id^='impfung_1['");

试试这个:

$elem = $("span[id^='impfung_1']");

【讨论】:

  • 单引号呢?
猜你喜欢
  • 2020-03-21
  • 2018-06-06
  • 2020-01-25
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多