Pass variable from link to div

空扰寡人 提交于 2020-01-15 09:26:09

问题


I have a pure css3 modal call by a link, and I wish to pass some variable to the modal.

Modal

    <div id="reasonmodal">
    <div class="modal-content">
        <div class="header">
            Reason
        </div>

        <div class="copy">
             //content
        </div>
        <div class="footer"><a href="?r=Register/UpdateReason&appid=">Close</a></div>
        <br />  

    </div>

HTML

    //some other code with foreach appid
    <a href="#reasonmodal">Click</a>

I want pass foreach appid to the modal, any suggestion to do that ? Thanks


回答1:


i have an idea (js only):

<a href="#reasonmodal" class="modal-link" rel="<?=$app_id?>" >Click</a>

<a href="#" id="submit-link">Close</a>

and modal action :

$(".modal-link").click(function(){
   $("#submit-link").attr('href','?r=Register/UpdateReason&appid='+$(this).attr('rel'));
   $($(this).attr('href')).modal('show');
});



回答2:


Just print the var

<div id="reasonmodal">
    <div class="modal-content">
        <div class="header">
            Reason
        </div>

        <div class="copy">
             //content
        </div>
        <div class="footer"><a href="?r=Register/UpdateReason&appid=<?php echo $appid; ?>">Close</a></div>
        <br />  

    </div>



回答3:


I see that you want to dynamically pass variables to the model each time click the html div. Just use ajax post to get these values and update the modal content before it is triggered

Modal

<div id="reasonmodal">
<div class="modal-content">
    <div class="header">
        Reason
    </div>

    <div class="copy" id="modal_content">
         //content
    </div>
    <div class="footer"><a href="?r=Register/UpdateReason&appid=">Close</a></div>
    <br />  

</div>

Add a onclick event to html div

<a href="#reasonmodal" onlick='updateModal()'>Click</a>

Implement the updateModal() function which will POST an ajax request to get data. Something like this:

function viewDetail()
{
    $.ajax({
        type: "POST",
        url: <?php echo "\"" . Yii::app()->createUrl('controller/action') . "\""; ?>,
        data: { param : "value"},   
    }).done(function(msg){
        //update the model content with msg responded from server
        document.getElementById("modal_content").innerHTML = msg;    
    });     
}

The modal will be triggered after the html content is updated.

(Give me a vote-up if it works for you)



来源:https://stackoverflow.com/questions/27143818/pass-variable-from-link-to-div

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!