gpt4 book ai didi

javascript - 获取选中的元素并用逗号分隔它们,最后一个元素用 "and"分隔

转载 作者:行者123 更新时间:2023-11-28 02:51:31 29 4
gpt4 key购买 nike

我想获取选中的元素并用逗号分隔它们,最后一个元素用“and”分隔

它的显示输出为:

Expected result

我想删除最后一个元素后面的逗号 (,) 并在它前面添加 'and',例如

Sugar, Milk and Extra chocolate

或者如果选择了两个:

Sugar and Milk

我的代码:-

<html>
<head>
<title>Assignment1</title>
<script>
function genresult() {
//Name
var name=document.getElementById("cname").value;

//Select drink
var seldrink="";
var drinks=document.getElementsByName("drink");
for(var i = 0; i < drinks.length; i++)
{
if(drinks[i].checked) {
seldrink=drinks[i].value;
break;
}
}

//Add ons
var addons="";
var addns=document.getElementsByName("addons");
for(var i = 0; i < addns.length; i++)
{
if(addns[i].checked) {
addons+=addns[i].value + ", " ;
}
}
document.getElementById("result").innerHTML = addons;

//Payment mode
var pay = document.getElementById("payment").value;

var final= "<span>"+name+" </span> would like to have <span>"+seldrink+" </span> <br/>\
<span> With"+ addons +" </span> <br/>\
will pay by <span>"+pay+" </span>.";

document.getElementById("result").innerHTML = final;
}
</script>
<style>
#result {
padding: 20px;
border: 1px solid #e5e5e5;
background: #FAD160;
font-size: 20px;
}
#result span {
color: #FFFFFF;
font-weight: bold;
text-decoration: underline;
text-transform: capitalize;
}
</style>
</head>
<body>

<table border="0" cellpadding="10" width="600" align="center">
<tr>
<td colspan="2"><h3>Order details</h3></td>
</tr>
<tr>
<td>Enter you name</td>
<td><input type="text" id="cname"/></td>
</tr>
<tr>
<td>Select your drink</td>
<td>
<input type="radio" id="coffee" name="drink" value="Coffee"/><label for="Coffee">Coffee</label>&nbsp;&nbsp;
<input type="radio" id="tea" name="drink" value="Tea"/><label for="Tea">Tea</label>&nbsp;&nbsp;
<input type="radio" id="chocolatedrink" name="Drink" value="Chocolatedrink"/><label for="chocolatedrink">Chocolatedrink</label>
</td>
</tr>
<tr>
<td>Select Add-ons</td>
<td>
<input type="checkbox" id="wsugar" name="addons" value="Sugar"/><label for="wsugar">With sugar</label>&nbsp;&nbsp;
<input type="checkbox" id="wmilk" name="addons" value="Milk"/><label for="wmilk">With milk</label>&nbsp;&nbsp;
<input type="checkbox" id="wchocolate" name="addons" value="Extra chocolate"/><label for="wchocolate">With extra chocolate</label>
</td>
</tr>
<tr>
<td>Select payment mode</td>
<td>
<Select id="payment"/>
<option>Credit card</option>
<option>Debit card</option>
<option>Cash</option>
</Select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" id="submit-btn" onclick="genresult();"/></td>
</tr>
<tr>
<td colspan="2"><p id="result">Order details as Below:</p></td>
</tr>
</table>
</body>
</html>

最佳答案

我会先抓取选中项的所有值并将它们存储在一个数组中。然后我将根据数组中的项目数使用 join 方法将数组转换为字符串。

var addns= Array.prototype.slice.call(document.getElementsByName("addons")).filter(function(el) {
return el.checked;
}).map(function(el){ return el.value});

if(addns.length) {
if(addns.length === 2) addns = addns.join(" and ");
else if(addns.length > 2) {
addns.splice(-2, 2, addns[addns.length-2]+ " and " + addns[addns.length -1]);
addns = addns.join(", ")
}
addns = "With " + addns;
}

关于javascript - 获取选中的元素并用逗号分隔它们,最后一个元素用 "and"分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39184029/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com