I'm trying to get a button to store the button value (id answer0, 'value2') as well as a drop down box selection (id answer1, 'value'. As the code is nothing is saving, though I have managed to save the button 'value2' in a previous attempt. Have been googling for 3 days to no avail. Any suggestions would be appreciated!
$('.btn-answer').off('click').on('click', function(evt) {
var answer0 = $(evt.target).attr("value2");
//var answer1 = $("#answer1").val();
var answer1 =$('#answer1 option:selected').text();
var all_answers = {};
all_answers['answer0'] = answer0;
all_answers['answer1'] = answer1;
if (typeof all_answers != 'undefined') {
console.log(answer0);
console.log(answer1);
pybossa.saveTask(task.id, all_answers).done(function() {
deferred.resolve();
<div>
<select name="answer1" class = "btn-success btn-mini">
<option value="none">Choose a Class</option>
<option>Woodland and Scrub</option>
<option value="A1">Woodland</option>
......
<option value="J5">Other Habitat</option>
</select>
</div>
<h1 id="question2"></h1> <!-- The question will be loaded here -->
<h2><i class="icon-asterisk"></i> </h2>
<div id="answer0"> <!-- Start DIV for the submission buttons -->
<!-- If the user clicks this button, the saved answer will be value2="Certai-->
<button class="btn btn-success btn-answer" value2 ="a"><i class="icon icon-white icon-thumbs-up"></i> Certain</button>
<!-- If the user clicks this button, the saved answer will be value2="Fairly Certain"-->
<button class="btn btn-answer btn-warning" value2="b"><i class=""></i> Fairly Certain</button>
<!-- If the user clicks this button, the saved answer will be value2="Fairly Certain"-->
<button class="btn btn-answer btn-danger" value2="c"><i class="icon icon-white icon-thumbs-down"></i> Unsure</button>
</div>
<P></P>
<!-- End of DIV for the submission buttons -->
Change this line:
var selected_answer1 = $("#answer1").val();
I guess $("#answer1")
is input
, and with $("#answer1").val("value")
you are trying to set its value. But you want to get its value.
To get value from drop down:
$('#id_of_drop_down').val();
will return the value, not the text.
If you want text of selected option
, use this:
$('#id_of_drop_down option:selected').text();
.
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.