[英]How to get values from option-list into consecutive input-fields by onclick-function
[英]How to get whole terms of more than 1 word from option-list into consecutive input-fields
我有一個html表單,可以從數據庫中讀取數據。 可以從選項列表中選擇DB請求的值,並將其發送到輸入字段。 根據需要,可以創建其他輸入字段。 當所選值是單個單詞時,此功能可以很好地與以下功能配合使用:
enter code here
<script>
// Start of function for additional input fields
var counter = 1;
var limit = 10;
function addInput(divName){
if (counter == limit) {
alert("You have reached the limit of adding " + counter + "
inputs");
}
else {
// Create new div
var newdiv = document.createElement('div');
newdiv.innerHTML = '<br><input type="text" name="productinput[]"
class="awesomplete" list="productinputselect" size="20"/>';
document.getElementById(divName).appendChild(newdiv);
// Re instantiate Awesomplete
new Awesomplete(newdiv.querySelector('input'), { list:
document.querySelector('#productinputselect') });
// Set counter
counter++;
}
}
// End of function for additional input fields
// Start of function for sending selected values to input-field
function getSelectValues(select) {
var result = [];
var options = select && select.options;
var opt;
for (var i=0, iLen=options.length; i<iLen; i++) {
opt = options[i];
if (opt.selected) {
result.push(opt.value || opt.text);
}
}
return result;
}
function sendproductnameinput() {
var index = 0;
var inputs = document.formdatabaseDE.elements["productinput[]"];
var selected = getSelectValues(document.formdatabase.productselect);
// set the input values based on the selected options
for (var i = 0; i < selected.length; i++) {
if(index < inputs.length) {
inputs[index].value = selected[i];
index++;
}
}
// empty the remaining inputs
for (var i = selected.length; i < inputs.length; i++) {
inputs[i].value = '';
}
}
// End of function for sending selected values to input-field
</script>
但是,如果選項列表中的值由2個或更多單詞組成(用單個空格分隔),則僅第一個單詞發送到輸入字段,而不是整個單詞。
如果有人可以幫助我修改此功能,以便將值完全發送到輸入字段(盡管它們包含多個單詞),我會感到非常高興。
該問題的解決方案是使用引號將單詞除以空格,將其分隔為1個項:
enter code here
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optproduct" value='. $row['Name'] . '>' . $row
['Name']. '</option>'; echo '<br>'; }
成為:
enter code here
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optproduct" value="'. $row['Name'] . '">' . $row
['Name']. '</option>'; echo '<br>'; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.