javascript - 在 Yii 中将 js 变量传递给模态

标签 javascript php jquery yii

我有一个
- 打开模式窗口的按钮,以及
- jquery dynatree。

如何将变量从 js(树中选定节点的 id)传递到 Yii 中的模态?

//the modal window
$this->widget('bootstrap.widgets.TbModal', array(
'id' => 'createExercise',
'header' => 'Create..',
'content' => $this->renderPartial('_form', ['model'=>$model], 1),
'footer' => [
    TbHtml::button('Save', ['onclick'=>'$("#exercise-form").submit()']),
 ],
));

//the button
TbHtml::button('Create', array(
'id' => '#btnCreate',
'data-toggle' => 'modal',
'data-target' => '#createExercise',
));

我使用以下代码从树中获取事件节点的 ID: $("#yw0").dynatree("getActiveNode").data.id;

如何将此 ID 传递给模式? 谢谢!

最佳答案

您可以使用 onClick 属性从按钮传递它 将按钮修改为这个

TbHtml::button('Create', array(
'id' => '#btnCreate',
'data-toggle' => 'modal',
'data-target' => '#createExercise',
'onClick'=>'js:PassToModal($("#yw0").dynatree("getActiveNode").data.id)',
));

在你的js函数中,你可以用值做你想做的事情,例如你想根据节点值用一些动态数据填充模态,你可以这样做

<script type="text/javascript">
function PassToModal(id){
        <?php echo CHtml::ajax(array(
            'url'=>$this->createurl('getDetails'),
            'type'=>'GET',
            'dataType'=>'html',
            'data'=>array('id'=>'js:id'),
            'update'=>'#createExerciseBody'// Id of the Modal Body
        )); ?>
}
</script>

关于javascript - 在 Yii 中将 js 变量传递给模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23885856/

相关文章:

javascript - jquery 代码工作正常,但其他单选按钮干扰其工作

php native 查询到codeigniter查询

php - 在下拉列表更改时重新加载页面并传递该值

javascript - 元素对 JQuery Click 没有反应

javascript - 在 jQuery 中调用 javascript 对象数组

javascript - 如何使用数据表在单击按钮时显示带有列数据的确认模态

javascript - 如何点击或双击网页上的某个词来触发事件处理程序?

javascript - 读取多个文件输入

javascript - 如何知道点击了哪个链接/按钮,然后替换内容?

php - 注意undefined_index,使用带有两个表的select来访问两个表的字段