【问题标题】:How do i get xeditable's content in js我如何在js中获取xeditable的内容
【发布时间】:2018-11-26 20:37:24
【问题描述】:

我有以下可编辑的代码:

 $this->widget('editable_ori.Editable', array(   
      'url' => $this->createUrl('index'),
      'pk' => Yii::app()->user->id,
      'name' => 'index',
      'type' => 'text',
      'text' => $data,
      'placement' => 'top',
      'showbuttons' => 'bottom',
      'send' => 'auto',
));

我想在我的 js 中获取该字段的内容。

我尝试使用下面的代码在 js 中获取此内容。但它不起作用

 $(".editable editable-click").text;

【问题讨论】:

  • 您想在提交后或加载后立即获取可编辑文本字段中的值吗?
  • @jmsds 加载完毕。
  • 请务必将答案标记为正确,这有助于您解决问题。它可能会帮助其他人寻找相同的解决方案。

标签: php jquery yii


【解决方案1】:
  • 您在类名editable-click 前缺少.

  • 在类名.editable 后面有一个额外的空格

  • 获取文本的方法是jquery中的text()而不是text属性。

改变

 $(".editable editable-click").text;

$(".editable.editable-click").text()

EDIT

您应该在加载可编辑文件后调用脚本,您可以使用事件onInit 从另一个依赖于可编辑输入的文件触发您的 javascript 函数。例如,如果你有一个函数

function myFunction(){
//do something
    $(".editable.editable-click").text()
//do something more
}

那么您应该尝试从onInit 事件中调用该函数,如下所示

$this->widget('editable_ori.Editable', array(   
      'url' => $this->createUrl('index'),
      'pk' => Yii::app()->user->id,
      'name' => 'index',
      'type' => 'text',
      'text' => $data,
      'placement' => 'top',
      'showbuttons' => 'bottom',
      'send' => 'auto',
      'onInit' => 'js:function(){myFunction();}'
));

【讨论】:

  • 如果我在控制台中使用这个命令,它会抓取数据。但是,如果我从我的 jquery 文件中打印,它会得到空记录。
  • @Cristal 你的文件在可编辑文件加载之前被调用,你是如何加载 js 文件的,在你的问题中更新。
【解决方案2】:

尝试使用可编辑的onShown 选项,该选项将在可编辑表单显示后立即执行。你可以参考下面的代码。

$this->widget('editable_ori.Editable', array(   
    'url' => $this->createUrl('index'),
    'pk' => Yii::app()->user->id,
    'name' => 'index',
    'type' => 'text',
    'text' => $data,
    'placement' => 'top',
    'showbuttons' => 'bottom',
    'send' => 'auto',
    'onShown' => 'js: function() {
        var txtVal=$(this).data("editable").value;  // This will hold your textbox value as soon as it opens.
        someFunction(txtVal);  //Call your JS function with textbox value as parameter.

    }'
));

然后在你的 JS 函数中,你可以这样做:

<script>
     function someFunction(txtVal)
     {
         //Do anything with editable text box value
     }
</script>

希望对你有所帮助。

【讨论】:

  • 当你点击显示的链接时显示作品,他想在页面加载后立即获取内容/文本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 2020-02-07
  • 1970-01-01
  • 2011-03-10
  • 2017-09-30
  • 2016-08-08
  • 2010-10-02
相关资源
最近更新 更多