Hej igen
jeg er gået med ideen om at sætte flere ting ind i listen så de alle er forskeldige, men man skal huske kun at lave 1 mellem rum mellem ting i listen, jeg syntes | ville være fedt, men det kan den ikke lide.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script src="
http://localhost/test6/jquery-2.1.3.js" crossorigin="anonymous"></script>
</head>
<body>
<form id="myForm">
<input list="answers" id="answer" onchange="shipnamechange()">
<datalist id="answers">
<?php
$res=mysqli_query($db,"select * from shiplist");
while($row=mysqli_fetch_assoc($res))
{
?>
<option data-value="<?php echo $row["id"]; ?>"><?php echo $row["samlet"]; ?></option>
<?php
}
?>
</datalist>
<input type="hidden" name="answer" id="answer-hidden">
</form>
<p>Submitted value (for debugging):</p>
<pre id="result"></pre>
<input type="text" id="name" placeholder="name">
<input type="text" id="class" placeholder="class">
<input type="text" id="pnt" placeholder="pnt">
<script>
function shipnamechange(){
var ajax = new XMLHttpRequest();
var method = "GET";
var x = document.getElementById("answer-hidden").value;
var url = "shipnamechange.php?shipname=" + x;
var asynchronous = true;
ajax.open(method, url, asynchronous);
ajax.send();
ajax.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
var data = JSON.parse(this.responseText);
console.log(data)
var html = "";
for (var a = 0; a < data.length; a++)
{
var skibsnavn = data[a].shipname;
var Classnavn = data[a].Classname;
var shiprole = data[a].shiprole;
var shippnt = data[a].shippnt;
var shipcountries = data[a].shipcountries;
}
document.getElementById("name").value = skibsnavn;
document.getElementById("class").value = Classnavn;
document.getElementById("pnt").value = shippnt;
document.getElementById("answer").value = skibsnavn;
}
}
}
</script>
<script>
$('input
').on('input', function(e) {
var $input = $(e.target),
$options = $('#' + $input.attr('list') + ' option'),
$hiddenInput = $('#' + $input.attr('id') + '-hidden'),
label = $input.val();
$hiddenInput.val(label);
for (var i = 0; i < $options.length; i++) {
var $option = $options.eq(i);
if ($option.text() === label) {
$hiddenInput.val($option.attr('data-value'));
break;
}
}
});
// For debugging purposes
$("#myForm").on('submit', function(e) {
$('#result').html($('#answer-hidden').val());
e.preventDefault();
});
</script>
</body>
</html>