te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Passing data to Bootstrap 3 Modal - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing data to Bootstrap 3 Modal - Stack Overflow

programmeradmin2浏览0评论

I have a table with a bunch of data, and I've placed a button at the end of each row that I want to use to trigger a modal and pass the unique id of that row into the modal.

I found this answer: Passing data to a bootstrap modal

But it doesn't appear to work with Bootstrap 3, and I can't find any data about this anywhere any ideas?

I'm using the same code listed:

Header:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href = "bootstrap/css/bootstrap.min.css" rel = "stylesheet">
<link href = "bootstrap/css/styles.css" rel = "stylesheet">
<script type="text/javascript">
$(document).on("click", ".open-AddBookDialog", function () {
var myBookId = $(this).data('id');
$(".modal-body #bookId").val(myBookId);
});
</script>
</head>

Body:

<a data-toggle="modal" data-id="ISBN" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>

Modal:

<div class="modal" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Modal header</h3>

</div>
<div class="modal-body">
<p>some content</p>
<input type="text" name="bookId" id="bookId" value="" />
</div>
</div>

The resulting page will open the modal, but the text field is blank.

I forgot to mention, I do have both jquery and bootstrap loaded:

<script src = ".10.2/jquery.min.js"></script>
<script src = "bootstrap/js/bootstrap.js"></script>

I have a table with a bunch of data, and I've placed a button at the end of each row that I want to use to trigger a modal and pass the unique id of that row into the modal.

I found this answer: Passing data to a bootstrap modal

But it doesn't appear to work with Bootstrap 3, and I can't find any data about this anywhere any ideas?

I'm using the same code listed:

Header:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href = "bootstrap/css/bootstrap.min.css" rel = "stylesheet">
<link href = "bootstrap/css/styles.css" rel = "stylesheet">
<script type="text/javascript">
$(document).on("click", ".open-AddBookDialog", function () {
var myBookId = $(this).data('id');
$(".modal-body #bookId").val(myBookId);
});
</script>
</head>

Body:

<a data-toggle="modal" data-id="ISBN" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>

Modal:

<div class="modal" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Modal header</h3>

</div>
<div class="modal-body">
<p>some content</p>
<input type="text" name="bookId" id="bookId" value="" />
</div>
</div>

The resulting page will open the modal, but the text field is blank.

I forgot to mention, I do have both jquery and bootstrap loaded:

<script src = "http://ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src = "bootstrap/js/bootstrap.js"></script>
Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Dec 21, 2013 at 20:13 bryanxbryanx 1691 gold badge3 silver badges11 bronze badges 4
  • You seem to be doing it right. To be absolutely sure, try console.log-ing both myBookId and $(".modal-body #bookId"). Do they contain what they should? – Lasse Commented Dec 21, 2013 at 20:17
  • I'm getting a Uncaught ReferenceError: $ is not defined which is referring to the line $(document).on("click", ".open-AddBookDialog", function () { – bryanx Commented Dec 21, 2013 at 21:10
  • if $ is not defined, then you haven't loaded jQuery onto the page yet. You'll need both jQuery and bootstrap's JS. – satchmorun Commented Dec 21, 2013 at 21:58
  • I have loaded jQuery, I even moved the scripts up into the header to see if that would solve it, but it didn't change anything :( – bryanx Commented Dec 21, 2013 at 23:16
Add a ment  | 

1 Answer 1

Reset to default 9

Try manually triggering the modal, rather than using the data API.

Update the hyperlink like so:

<a data-id="ISBN" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>

Then adjust your jQuery code:

$(document).on("click", ".open-AddBookDialog", function (e) {

    e.preventDefault();

    var _self = $(this);

    var myBookId = _self.data('id');
    $("#bookId").val(myBookId);

    $(_self.attr('href')).modal('show');
});

Seems to work for me; jsFiddle @ http://jsfiddle/4XJ54/

发布评论

评论列表(0)

  1. 暂无评论