这是我的html代码
/* 4. Make the request */
let task = session.dataTaskWithRequest(request) {data, response, downloadError in
if let error = downloadError {
println("Could not complete the request \(error)")
} else {
/* 5. Parse the data */
var parsingError: NSError? = nil
let parsedResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &parsingError) as! NSDictionary
/* 6. Use the data! */
if let error = parsingError {
println(error)
} else {
if let results = parsedResult["results"] as? [[String : AnyObject]] {
self.movies = Movie.moviesFromResults(results)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
} else {
println("Could not find results in \(parsedResult)")
}
}
}
}
/* 7. Start the request */
task.resume()
这是我的jquery代码
<div class="panel-footer">
<div class="input-group">
<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend">
<span class="input-group-btn">
<button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="79">Send</button>
</span>
</div>
<div class="input-group">
<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend">
<span class="input-group-btn">
<button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="80">Send</button>
</span>
</div>
</div>
坚持使用id,它始终显示为未定义。
我如何获得按钮值
答案 0 :(得分:1)
我已插入代码。这里是id没有给出未定义的
$(document).on('keypress', '.message', function (e) {
if (e.which == 13) {
var msg = $(this).val();
var id = $(this).next('span').find('button.btn_chat').val();
alert(msg);
alert(id);
}
e.preventDefault();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel-footer">
<div class="input-group">
<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend">
<span class="input-group-btn">
<button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="79">Send</button>
</span>
</div>
<div class="input-group">
<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend">
<span class="input-group-btn">
<button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="80">Send</button>
</span>
</div>
</div>
&#13;
答案 1 :(得分:0)
跨度是下一个兄弟,按钮是跨度的后代,因此使用closest和next将不起作用。
closest()用于查找祖先元素,next()用于查找下一个兄弟元素。
在您的情况下,您应使用.next()查找范围并使用.find()查找后代按钮
var id = $(this).next('span').find('button.btn_chat').val();
演示:Fiddle
另一个更简单的解决方案是使用data-*
属性将值存储在输入元素本身中,如
<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend" data-id="79">
然后
var id = $(this).data('id');
答案 2 :(得分:0)
$(this).closest('span')
将搜索文本框中最近的父span标记,而不是您需要相邻的span标记。尝试将next()
与find()
:
$(function() {
$('input:text.message').on('keypress', function(e) {
if (13 !== e.which) return;
var btn = $(this).next().find('.btn_chat');
console.log(this.value, btn.val());
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="panel-footer">
<div class="input-group">
<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend">
<span class="input-group-btn">
<button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="79">Send</button>
</span>
</div>
<div class="input-group">
<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend">
<span class="input-group-btn">
<button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="80">Send</button>
</span>
</div>
</div>
&#13;
答案 3 :(得分:0)
尝试使用$(this).parent()。nearest('span')。next('button.btn_chat')。val();