我想在此之后只选择文本框的id以插入数据库。例如,我有echo $t['id']
echo $t['name']
,但我想只选择id。有什么解决方案吗?我想从li元素中得到$ t ['id']之后插入数据库...如果我只写$ t ['id']我可以插入数据库但是如果我写的话$ t ['id'] - $ t ['name']我收到错误...所以我只需从文本框中获取$ t ['id']
HTML:
<input type="text" name="dropdiv" id="dropdiv" style="width:605" />
<div id="dragdiv">
<ul id="allItems">
<?php if($tags): ?>
<?php foreach($tags as $t): ?>
<li id="node" runat="server" value="$t['id']">
<?php echo $t['id']?><?php echo $t['name'] ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
<br />
JQuery的:
<script type="text/javascript">
$(function() {
$("#dragdiv li").draggable({
helper: "clone",
cursor: "move",
revert: "invalid"
});
initDroppable($("#dropdiv"));
function initDroppable($elements) {
$elements.droppable({
activeClass: "ui-state-default",
hoverClass: "ui-drop-hover",
accept: ":not(.ui-sortable-helper)",
over: function (event, ui) {
var $this = $(this);
},
drop: function (event, ui) {
var $this = $(this);
if ($this.val() == '') {
$this.val(ui.draggable.text().trim());
ui.draggable.remove();
} else {
$this.val($this.val() + " " + ui.draggable.text().trim());
ui.draggable.remove();
}
}
});
}
});
</script>
笨
$tags = $this->input->post('dropdiv');
$theid = $this->db->insert_id();
$tags_arr = array();
$theid = $this->db->insert_id();
$tags_arr = explode(' ',$tags);
foreach ($tags_arr as $t)
{
$tag_SQL = $t;
$this->db->query("insert into tags_in_news (news_id, tag_id) values ('{$theid}','{$tag_SQL}')");
}
请帮帮我......
答案 0 :(得分:0)
我在代码中只看到一个文本框。所以,就这样做:
$("#dropdiv").method();
在此处阅读有关jQuery选择器的更多信息:http://api.jquery.com/category/selectors/
答案 1 :(得分:0)
我想我慢慢开始明白了。将ui.draggable.text()
替换为ui.draggable.attr('value')
,因为您正试图解决此问题。但更好的是,用html中的value
属性替换data-id
属性(这样它是正确的html)。
HTML:
<li id="node" runat="server" data-id="$t['id']">
JS:
if ($this.val() == '') {
$this.val(ui.draggable.attr('data-id').trim());
ui.draggable.remove();
} else {
$this.val($this.val() + " " + ui.draggable.attr('data-id').trim());
ui.draggable.remove();
}