如何通过JavaScript函数从select选项中一次获取值?
这是我的代码:
<select name="ledger" id="ledger" onchange="setDebit(this.value)" required>
<option value="" >Select</option>
<?php
$ledgerResult = $this->db->query('SELECT * FROM ledger WHERE account_type = "Creditors" ORDER BY name');
$ledgerData = $ledgerResult->result();
for ($c = 0; $c < count($ledgerData); ++$c) {
$ledger_id = $ledgerData[$c]->id;
$ledger_name = $ledgerData[$c]->name;
$ledger_credit = $ledgerData[$c]->credit; ?>
<option value="<?php echo $ledger_id;?>"><?php echo $ledger_name;?></option>
}
</select>
<script>
function setDebit(ele){
document.getElementById("set_debit").value = ele;
}
</script>
我收到$ledger_id
并通过setDebit()
将此值发送到脚本。但我需要的是发送$ledger_credit
。我可以将其设置为选项值而不是$ledger_id;
,但我还需要将select
的值设为$ledger_id
。
如何将$ ledger_id设置为选项值,但是通过setDebit(this.value)
发送$ ledger_credit?
答案 0 :(得分:1)
describeTopics
答案 1 :(得分:0)
您必须在HTML部分中将属性设置为多个: http://www.w3schools.com/tags/att_select_multiple.asp
之后,您可以查看此问题以获取更多信息: How to get all selected values of a multiple select box using JavaScript?
答案 2 :(得分:0)
您可以在您的选项中添加属性PM
replace()
在function convertTime12to24(time12h) {
const [time, modifier] = time12h.split(' ');
let [hours, minutes] = time.split(':');
if (hours === '12') {
hours = '00';
}
if (modifier === 'PM') {
hours = parseInt(hours, 10) + 12;
}
return hours + ':' + minutes;
}
function formatTime(i_string) {
console.log(i_string.replace(/([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])(PM)/gi, function newDate(x) {
return convertTime12to24(x.replace("PM", " PM"))
}));
}
formatTime("The time is now 4:15PM");
formatTime("The time is now 12:15PM");
formatTime("The time is now 4:00AM");
formatTime("The time is now 12:00AM");
formatTime("The time is now 11:00PM");
函数中:
data-credit
您必须在此解决方案中使用jquery
答案 3 :(得分:0)
如果你没有使用jquery。
<option value="<?php echo $ledger_id."||".$ledger_credit; ?><?php echo $ledger_name;?> </option>
<script> function setDebit(){ var details = document.getElementById("ledger").value; var allData = details.split("||"); var ledger_id = allData[0]; var ledger_credit = allData[1]; console.log(ledger_id); console.log(ledger_credit); } </script>