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

时间:2016-08-27 17:44:05

标签: javascript html arrays forms

我想获取已检查的元素,并使用逗号分隔它们,并使用'和'

分隔最后一个元素

显示输出为:

Expected result

我想在最后一个元素后删除逗号(,)并添加'和'在它之前

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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

我建议使用数组来执行此任务。

首先,收集数组中的所有项目。然后获取最后两个元素并将它们与' and '连接起来,并将它作为最后一个元素放回到数组中,然后将所有元素与', '连接起来以获得一个很好的字符串。

方法

此提案适用于任何长度的数组,即使只有一个或两个元素。

var array = ['tea', 'milk', 'honey'],
    string = array.concat(array.splice(-2, 2).join(' and ')).join(', ');

console.log(string);

答案 1 :(得分:0)

我首先获取已检查项的所有值并将它们存储在数组中。然后我将根据数组中的项目数使用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; 
        }