如何将JS单选按钮值转为NBA 2K21随机建模的位置生成算法参数
实现方案
第一步:修改HTML单选按钮配置
给每个身高单选按钮补充value属性,用于获取选中的身高值,修改后代码如下:
<h1> 2k Archetype Generator </h1> <p> Choose Height </p> <form> <input name="height" type="radio" id="h1" value="6'2\""> 6'2" <br> <input name="height" type="radio" id="h2" value="6'3\""> 6'3" <br> <input name="height" type="radio" id="h3" value="6'4\""> 6'4" <br> <input name="height" type="radio" id="h4" value="6'5\""> 6'5" <br> <input name="height" type="radio" id="h5" value="6'6\""> 6'6" <br> <input name="height" type="radio" id="h6" value="6'7\""> 6'7" <br> <input name="height" type="radio" id="h7" value="6'8\""> 6'8" <br> <input name="height" type="radio" id="h8" value="6'9\""> 6'9" <br> <input name="height" type="radio" id="h9" value="6'10\""> 6'10" <br> <input name="height" type="radio" id="h10" value="6'11\""> 6'11" <br> </form> <br> <button id="positionchoice" onclick="randpos()"> Position </button> <p id="pos"> </p> <button id="primary" onclick="randprim()"> Primary </button> <p id="no1"> </p> <button id="secondary" onclick="randsecond()"> Secondary </button> <p id="no2"> </p>
第二步:实现randpos()函数
替换你代码中空的randpos函数为以下实现:
function randpos() { // 获取选中的身高单选框 const selectedHeight = document.querySelector('input[name="height"]:checked'); // 未选中身高给出提示 if (!selectedHeight) { p.innerHTML = "请先选择身高"; return; } // 将身高字符串转换为英寸数值,方便区间判断 const heightVal = selectedHeight.value; const [feet, inchPart] = heightVal.split("'"); const totalInch = parseInt(feet) * 12 + parseInt(inchPart.replace('"', '')); let possiblePositions; // 按区间匹配可选位置 if (totalInch >= 74 && totalInch < 77) { // 6'2" ~ 6'4" 对应PG/SG possiblePositions = ['PG', 'SG']; } else if (totalInch >=77 && totalInch < 80) { // 6'5" ~ 6'7" 对应SG/SF possiblePositions = ['SG', 'SF']; } else if (totalInch >=80 && totalInch < 82) { // 6'8" ~ 6'9" 对应SF/PF possiblePositions = ['SF', 'PF']; } else if (totalInch >=82 && totalInch <=83) { // 6'10" ~ 6'11" 对应PF/C possiblePositions = ['PF', 'C']; } // 随机取一个位置输出 p.innerHTML = possiblePositions[Math.floor(Math.random() * possiblePositions.length)]; }
内容的提问来源于stack exchange,提问作者user16489197
相关产品推荐
相关产品推荐

