使用jQuery获得span的值

时间:2022-11-19 19:34:07

Essentially what I'm trying to figure out is how to retrieve the value a span(id="camera_status") that's been populated with the name of a image after its been captured using phonegap.

本质上,我想要弄清楚的是如何检索一个被使用phonegap捕获的图像名称的span(id="camera_status")。

The way I want it to work is that the image name will then be uploaded to the database along with the form information that's submitted. At the moment it seems to be reading the initial blank value before the span is populated with the image name. If there's anyone who know's how I can fix this i would be grateful.

我希望它工作的方式是,然后将图像名称连同提交的表单信息一起上载到数据库。目前,它似乎是在使用图像名称填充span之前读取初始的空白值。如果有人知道我该如何解决这个问题,我会很感激。

HTML and Javascript of form page

表单页面的HTML和Javascript

<div>
  <input type="button" onclick="takePicture();" value="Take Picture" /><br/>
</div>
<div>
<div>
    <b>Status:</b> <span id="camera_status"></span><br>
    <b>Image:</b> <img style="width:240px;visibility:hidden;display:none;" id="camera_image" src="" />
</div>      
<div id="landmark-1" data-landmark-id="1">
<form id="uploadForm">
    <label for="email">
        <b>Email</b>
        <input type="email" id="email" name="email">
    </label>

    <label for="comment">
        <b>Comment</b></br>
        <input id="comment" name="comment" cols="30" rows="10"></textarea>
    </label>
    <input type="button" onclick="uploadPicture();" value="Upload New location" />  
    <input type="submit" value="Upload New location" />

    </form>

<script>
var spanValue = $('#camera_status').html()

$(function(){

$('#uploadForm').submit(function(){
    var landmarkID = $(this).parent().attr('data-landmark-id');
    var postData = $(this).serialize();

    $.ajax({
        type: 'POST',
        data: postData+'&lid='+spanValue,
        //change the url for your project
        url: 'save.php',
        success: function(data){
            console.log(data);
            alert('Your comment was successfully added');
        },
        error: function(){
            console.log(data);
            alert('There was an error adding your comment');
        }
    });

    return false;
 });
});

</div>
</div>
</div>

2 个解决方案

#1


3  

Get the spanValue when you're actually going to use it, and not outside the DOM ready function, before anything is loaded :

当你实际要使用它时,得到它的值,而不是在DOM准备函数之外,在加载任何东西之前:

$(function(){
    $('#uploadForm').on('submit', function(e){
        e.preventDefault();
        var landmarkID = $(this).parent().data ('landmark-id'),
            postData   = $(this).serialize(),
            spanValue  = $('#camera_status').text()    

        $.ajax({
            type: 'POST',
            data: postData+'&lid='+spanValue,
            url: 'save.php'
        }).done(function(data) {
            console.log(data);
        });
     });
});

#2


6  

Try .text() :

尝试。text():

var spanValue = $('#camera_status').text();

#1


3  

Get the spanValue when you're actually going to use it, and not outside the DOM ready function, before anything is loaded :

当你实际要使用它时,得到它的值,而不是在DOM准备函数之外,在加载任何东西之前:

$(function(){
    $('#uploadForm').on('submit', function(e){
        e.preventDefault();
        var landmarkID = $(this).parent().data ('landmark-id'),
            postData   = $(this).serialize(),
            spanValue  = $('#camera_status').text()    

        $.ajax({
            type: 'POST',
            data: postData+'&lid='+spanValue,
            url: 'save.php'
        }).done(function(data) {
            console.log(data);
        });
     });
});

#2


6  

Try .text() :

尝试。text():

var spanValue = $('#camera_status').text();