2016-02-05 98 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="">

相關問題