我想通过使用jquery单击链接来传递输入字段值。这是我的
<a href="<?php echo base_url().$row->file_path; ?>" target="_blank" class="download"><input type="hidden" name="file_id" id="file_id" value="<?php echo $row->id; ?>"><?php echo $row->file_name; ?></a>
这是我的jquery代码
$(document).ready(function(){
$(".download").click(function(){
//alert('<?php //echo site_url('admin_dashboard/data'); ?>');
//return false;
var data= document.getElementById('file_id');
$.post("<?php echo site_url('admin_dashboard/data'); ?>",{ data:data},
function(ajaxresult){
$("#postrequest").html(ajaxresult);
});
});
});
这是通过使用jquery点击链接将值传递给其他页面的正确方法吗?如果是,那么我错了?如果没有可能的方法是什么。请指导我完成这个......
答案 0 :(得分:0)
在此处添加.value
:
var data= document.getElementById('file_id').value;
正如我所知,您在脚本中使用ajax方法,然后您必须使用e.preventDefault()
来停止页面导航:
$(".download").click(function(e){ // pass the event as arg
e.preventDefault(); // stop the page to navigate
答案 1 :(得分:0)
您尝试传递DOM元素本身,而不是值...... document.getElementById('file_id').value
应该为您提供所需内容。
答案 2 :(得分:0)
我建议你在index.html中这样做 在你的窗口范围之前
<script>
var SITE_URL = <?= site_url() ?>, BASE_URL = <?= base_url() ?>;
</script>
</body>
像这样你不需要在你的html中编写php,并将你的js放在.js文件中以构建更好的应用程序。
例如,将参数传递给Web应用程序的新实例您还将HTML对象作为数据发送,而不是他的值。
要在获得var时使用.value
来获取值;
答案 3 :(得分:0)
抓住价值而不是元素:
document.getElementById('file_id').value
更好,因为你正在使用jQuery:
$('#file_id').val()