简体   繁体   中英

On Clicking getting value of an input field using Jquery

I want to pass an input field value via clicking a link using jquery. Here is my

<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>

here is my jquery code

$(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);
            });
        });
    });

Is this the right way to pass a value to the other page via clicking a link using jquery? If yes then where i am getting it wrong? if not what are the possible ways to do so. Kindly guide me through this...

add .value here:

var data= document.getElementById('file_id').value;

As i see you are using ajax method in your script then you have to use e.preventDefault() to stop the page navigation:

$(".download").click(function(e){ // pass the event as arg
    e.preventDefault(); // stop the page to navigate

您正在尝试传递DOM元素本身,而不是值... document.getElementById('file_id').value应该给您您所需要的。

I would recommend you to do that in your index.html In your window scope right before

<script>
    var SITE_URL = <?= site_url() ?>, BASE_URL = <?= base_url() ?>;
</script>
</body>

Like this you don't need to write php in your html, and place your js in .js files to build better application.

for example , pass the argument to the new instance of your web app

You are also sending as data the HTML object and not his value. To get is value use .value when assing the var;

Grab the value rather than the element:

document.getElementById('file_id').value

Better, since you're using jQuery:

$('#file_id').val()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM