我有一个页面显示当地咖啡馆的列表。当用户点击某个咖啡馆时,显示模态对话框,其已经预填充了“咖啡馆名称”。页面包含许多咖啡馆名称,表单应该包含他点击的“咖啡馆名称”。
<table class="table table-condensed table-striped"> <tbody> <tr> <td>B&Js </td> <td>10690 N De Anza Blvd </td> <td> <a class="btn btn-primary" data-toggle="modal" onClick="$('#createFormId').modal('show')" >Announce</a> </td> </tr> <tr> <td>CoHo Cafe </td> <td>459 Lagunita Dr </td> <td> <a class="btn btn-primary" data-toggle="modal" onClick="$('#createFormId').modal('show')" >Announce</a> </td> </tr> <tr> <td>Hot Spot Espresso and Cafe </td> <td>1631 N Capitol Ave </td> <td> <a class="btn btn-primary" data-toggle="modal" onClick="$('#createFormId').modal('show')" >Announce</a> </td> </tr> </tbody> </table>
这里是模态形式
<div class="modal hide fade" id="createFormId""> <div class="modal-header"> <a href="#" class="close" data-dismiss="modal">×</a> <h3>Create Announcement</h3> </div> <div class="modal-body"> <form class="form-horizontal" method="POST" commandName="announceBean" action="/announce" > <input type="hidden" name="cafeId" value="104" /> <fieldset> <div class="control-group"> <label class="control-label" for="cafeName">Where I am Coding</label> <div class="controls"> <input id="cafeName" name="cafeName" class="input-xlarge disabled" type="text" readonly="readonly" type="text" value="B&Js"/> </div> </div> <div class="control-group"> <label class="control-label" for="date">Date</label> <div class="controls"> <input type="text" class="input-xlarge" id="date" name="date" /> <p class="help-block"></p> </div> </div> <div class="control-group"> <div class="controls"> <input type="submit" class="btn btn-primary" value="create" /> <input type="button" class="btn" value="Cancel" onclick="closeDialog ();" /> </div> </div> </fieldset> </form> </div>
问题是如何将实际值传递到模态形式的“value”属性?
<input type="hidden" name="cafeId" value="104" />
“show”事件的形式由“onlick”事件触发
<a class="btn btn-primary" data-toggle="modal" onClick="$('#createFormId').modal('show')" >Announce</a>
解决方法
你可以这样做:
<a class="btn btn-primary announce" data-toggle="modal" data-id="107" >Announce</a>
然后使用jQuery绑定点击并发送Announce data-id作为模式#cafeId中的值:
$(document).ready(function(){ $(".announce").click(function(){ // Click to only happen on announce links $("#cafeId").val($(this).data('id')); $('#createFormId').modal('show'); }); });