HTML:
HTML:
<select id="s2_basic" class="form-control" id="userid" name="userid">
<option value="" selected>-- Choose option --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Javascript:
Javascript:
function submitForm() {
var userid = $('#userid').attr('selected', 'selected');
$.ajax({
type: "POST",
url: "submit.php",
data: "userid=" + userid,
success: function(text) {
if (text == "success") {
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
Submit PHP returns error message:
提交PHP返回错误消息:
Value: ([object Object])
I have also tried
我也试过
var userid = $('#userid').val();
var userid = $('#userid option').val();
Still, the option value never gets passed correctly. What am I doing wrong?
不过,选项值永远不会被正确地传递。我做错了什么?
I want just the value="x" number to be posted as userid=x.
我只想让值="x"号作为userid=x。
2 个解决方案
#1
2
remove id="s2_basic"
from your select.
从您的选择中删除id="s2_basic"。
change :
变化:
<select id="s2_basic" class="form-control" id="userid" name="userid">
to
来
<select class="form-control" id="userid" name="userid">
#2
0
$('#userid').on('change',function(){
console.log('value '+$('#userid option:selected').val())
console.log('text '+$('#userid option:selected').text())
console.log('index '+$('#userid option:selected').index())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select class="form-control" id="userid" name="userid">
<option value="" selected>-- Choose option --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
try this
试试这个
#1
2
remove id="s2_basic"
from your select.
从您的选择中删除id="s2_basic"。
change :
变化:
<select id="s2_basic" class="form-control" id="userid" name="userid">
to
来
<select class="form-control" id="userid" name="userid">
#2
0
$('#userid').on('change',function(){
console.log('value '+$('#userid option:selected').val())
console.log('text '+$('#userid option:selected').text())
console.log('index '+$('#userid option:selected').index())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select class="form-control" id="userid" name="userid">
<option value="" selected>-- Choose option --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
try this
试试这个