2016-02-05 100 views
4

我一直在尝试从我的窗口模态子窗口传递任何值到父窗口,但它不起作用。 modal不会hide或当我单击Save时将值发送到父窗口。如何将价值从一个助推器模态孩子传递给父母?

application.php

<body> 
<h3 class="h3 black">Application</h3> 
<form class="form-horizontal" method="post" action="admin-controller.php" name="f1"> 
<input type="text" name="p_name" id="n1" > 
<?php 
    include 'includes/calendar.php'; 
    $calendar = new Calendar(); 
    echo $calendar->show(); 
?> 
</form> 
</body> 
<script src="https://code.jquery.com/jquery-2.1.0.min.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0 /jquery.validate.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 
<script type="text/javascript"> 
function post_value(){ 
    window.opener.document.f1.p_name = document.myform.skill_id_t.value; 
    $('#modal_1').modal('hide'); 
} 
</script> 

calendar.php

<?php 
class Calendar {  
public function show() { 
    //some code 
    $html .=$this->_showDay($someValues); 
    //some code  
} 

private function _showDay($cellNumber){  
    // Build up the html. 
    $html = '<td id="' . $this->currentDate . '" class="td-top-text ' . $classNameOne . $classNameTwo . '">'; 
    //Radio button that open the Modal window 
    $html .= '<input type="radio" data-toggle="modal" data-target="#modal_1" name="schedule_id" value="'. $shedule_id.'">'; 
    //singleModal 
    $html .=$this->_singleModal(); 
    $html .= '</td>';  
    return $html; 
} 

//child window 
public function _singleModal(){ 
    //Single Modal 
    $html = '<form name="myform" method="post" action="">'; 
    $html .= '<div class="modal fade" id="modal_1" role="dialog">';//Modal open 
     $html .= '<div class="modal-dialog">';//Modal Dialog open 
      $html .= '<div class="modal-content">';//Modal-Content Open 
       $html .= '<div class="modal-header">';//Modal-Header open 
        $html .= '<button type="button" class="close" data-dismiss="modal">&times;</button>';//bbutton 
        $html .= '<h4 class="modal-title bold cyan">Sign Up (Single)</h4>';//Title H4 
       $html .= ' </div>';//Modal-header Close  
       $html .= '<div class="modal-body">';//Modal-body open 

       //Type something to pass to parent window 
       $html .= '<input name="skill_id_t" type="text">'; 

       $html .= '</div>';//Modal-body close 

       $html .= '<div class="modal-footer">';//Footer open 
        $html .= '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>';//button 
        $html .= '<input onclick="post_value();" id="save" type="button" name="save-details" value="Save">';//input 
       $html .= '</div>';//Footer Close 
      $html .= '</div>';//Modal-content close 
     $html .= '</div>';//Modal Dialog Close 
    $html .= '</div>';//Modal close 
    $html .= '</form>'; 
    return $html; 
    } 
    } 

我读其他职位,据他们说,问题可能是因为javascript我不能使用data-target="#modal_1"javascript如此我试图删除target,并且只需将onclick调用函数调入我的<input name="schedule_id">要做$(#modal_1).modal('show');但仍然$(#modal_1).modal('hide');不起作用。

我也有标签php因为根据其他职位,我需要将javascript包括到我phpcalendar.php),而不是在我的application.php

编辑 我也我点击每一次得到了一些错误Save

TypeError: opener is null

试图通过增加window.opener...修复它,但它没有工作,要么

新编辑:

我不得不改变我的js代码这样:

$(function() { 
$('#btnLaunch_1').click(function() { 
    $('#modal_1').modal('show'); 
}); 

$('#btnSave_1').click(function() { 
    opener.document.f1.p_name = document.myform.skill_id_t.value; 
    $('#modal_1').modal('hide'); 
}); 

$('#cancel_1').click(function() { 
    $('#modal_1').modal('hide'); 
}); 
}); 

,我删除必须与data-target包括关闭窗口data-dismiss之一的一切。而且,我意识到问题是这条线opener.document.f1.p_name = document.myform.skill_id_t.value;由于某种原因,通过给出上述错误这条线路失败。 ...opener is null

Pleaseeee helpp =/II我在这里努力工作....

+0

我想在POST_VALUE您可能需要将该值赋给* p_name.value *而不是* p_name *。 – AverageUnknown

+0

@AverageUnknown感谢您的评论。我改变了,但仍然说'开启者为空' –

+0

是否有你使用* window.opener.etc *而不是像* $('[name =“p_name”]')*之类的原因?我不熟悉你使用的语法。 – AverageUnknown

回答

0

因此,在搜索越来越多并遵循第一条评论的一些建议之后,我相信从引导模态子窗口获取值并将其添加到父窗口的最佳方法是使用jQuery这种方式:

$(function() { 

//open modal 
$('#btnLaunch_1').click(function() { 
    $('#modal_1').modal('show');//no need data-target just <input id=""> 
}); 

$('#btnSave_1').click(function() { 

    //Get the value from the child into a variable 
    value = $('[name="skill_id_1"]').val(); 

    //Set the value to the parent window 
    $('[name="p_name"]').val(value); 

    //make the modal dismiss (no need data-target) 
    $('#modal_1').modal('hide'); 
}); 

    //if user click cancel instead of use data-dismiss, just hide it 
$('#cancel_1').click(function() { 
    $('#modal_1').modal('hide'); 
}); 

}); 

如果你找到一个更好的解决方案,这其中,请让我知道

感谢

0

你的首战表单名称是错误的;

变化

window.opener.document.f1.p_name

window.opener.document.myform.p_name

你参考

<form name="myform" method="post" action="">

相关问题