使用 jQuery 从跨度中获取价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16365271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting value from span with jQuery
提问by user2236497
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.
基本上我想弄清楚的是如何检索一个 span(id="camera_status") 的值,该值在使用 phonegap 捕获后填充了图像的名称。
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.
我希望它工作的方式是图像名称将与提交的表单信息一起上传到数据库。目前它似乎在用图像名称填充跨度之前读取初始空白值。如果有人知道我如何解决这个问题,我将不胜感激。
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>
采纳答案by adeneo
Get the spanValue
when you're actually going to use it, and not outside the DOM ready function, before anything is loaded :
spanValue
在加载任何内容之前获取您实际要使用它的时间,而不是在 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);
});
});
});
回答by Mohamed Ali
Try .text()
:
尝试.text()
:
var spanValue = $('#camera_status').text();