为什么我的表单输入值的更改没有出现在Javascript中?(Why is the change to my form input value not appearing in Javascript?)
我正在编写一个脚本来计算客户订购啤酒箱的成本。 有一个动态生成啤酒选项的选择菜单,客户必须从中选择,其中包括商品名称和价格。 用户选择其数量并单击“添加项目”按钮。 单击此按钮时,应将项目的名称和价格添加到“项目描述”列下的第一个可用输入中。
问题是不会出现对“descriptionInput0”输入的更改。 当我将“firstAvailable”变量记录到控制台时,更改将显示在那里,但不会出现在浏览器中。 我不确定发生了什么事。 有任何想法吗? 谢谢你的时间。
这是一个jsFiddle和代码。
window.onload = calculator(); function calculator() { var descriptions = new Array("Shamrock Ale", "Lucky Pilsner", "Irish Wheat", "Irish Malt", "Shamrock Rye"); var prices = new Array(24.99, 27.99, 27.99, 32.99, 35.99); populateSelectMenu(descriptions, prices); var descriptionMenu = document.getElementById("descriptionMenu"); addButton = document.getElementById("addItem"); addButton.onclick = function () { firstAvailable = detectAvailable(); firstAvailable = document.getElementsByName("descriptionInput" + firstAvailable); console.log(firstAvailable); firstAvailable.value = descriptionMenu.options[descriptionMenu.selectedIndex].text; } } function populateSelectMenu(descriptions, prices) { var descriptionMenu = document.getElementById("descriptionMenu"); for (var i = 0; i < descriptions.length; i++) { option = document.createElement("option"); option.value = i; option.innerHTML = descriptions[i] + ": $" + prices[i]; descriptionMenu.appendChild(option); } } function detectAvailable() { var descriptionInputs = document.querySelectorAll("input[name^='descriptionInput']"); inputs: for (var i = 0; i < descriptionInputs.length; i++) { if (descriptionInputs[i].value != '') { continue inputs; } else { var firstAvailable = i; break inputs; } } return firstAvailable; }
<form name="calculator" id="calculator"> <div id="userInput"> <select name="descriptionSelect" id="descriptionMenu"></select> <label>Quantity</label> <input type="number" name="quantity" min="1" max="10" size="2" value="1"> <input type="button" value="Add to List" id="addItem"> </div> <table align="center" cellspacing="1" cellpadding="5" width="40%" border="1"> <thead> <tr> <th>Item Description</th> <th>Unit Price</th> <th>Quantity</th> <th>Price</th> </tr> </thead> <tbody> <tr> <td> <input type="text" name="descriptionInput0" value="" size="30"> </td> <td> <input type="text" name="uPriceInput0" value="" size="5"> </td> <td> <input type="text" name="quantityInput0" value="" size="5"> </td> <td> <input type="text" name="tPriceInput0" value="" size="5"> </td> </tr> <tr> <td> <input type="text" name="descriptionInput1" value="" size="30"> </td> <td> <input type="text" name="uPriceInput1" value="" size="5"> </td> <td> <input type="text" name="quantityInput1" value="" size="5"> </td> <td> <input type="text" name="tPriceInput1" value="" size="5"> </td> </tr> <tr> <td> <input type="text" name="descriptionInput2" value="" size="30"> </td> <td> <input type="text" name="uPriceInput2" value="" size="5"> </td> <td> <input type="text" name="quantityInput2" value="" size="5"> </td> <td> <input type="text" name="tPriceInput2" value="" size="5"> </td> </tr> <tr> <td> <input type="text" name="descriptionInput3" value="" size="30"> </td> <td> <input type="text" name="uPriceInput3" value="" size="5"> </td> <td> <input type="text" name="quantityInput3" value="" size="5"> </td> <td> <input type="text" name="tPriceInput3" value="" size="5"> </td> </tr> <tr> <td> <input type="text" name="descriptionInput4" value="" size="30"> </td> <td> <input type="text" name="uPriceInput4" value="" size="5"> </td> <td> <input type="text" name="quantityInput4" value="" size="5"> </td> <td> <input type="text" name="tPriceInput4" value="" size="5"> </td> </tr> <tr> <td colspan="3" align="right">Total Price:</td> <td> <input type="text" name="totalPrice" size="5" disabled> </td> </tr> </tbody> </table> </form>
I am writing a script that calculates the cost of a customer's order of beer cases. There is a select menu of dynamically generated options of beer the customer has to choose from which includes the name of the item and it's price. The user selects their quantity and clicks the "Add Item" button. When this button is clicked, the item's name and price should be added to the first available input under the "Item Description" column.
The problem is that the changes to the "descriptionInput0" input do not appear. When I log the "firstAvailable" variable to console, the changes appear there, but not in the browser. I'm not really sure what's going on. Any ideas? Thanks for your time.
Here is a jsFiddle and the code.
window.onload = calculator(); function calculator() { var descriptions = new Array("Shamrock Ale", "Lucky Pilsner", "Irish Wheat", "Irish Malt", "Shamrock Rye"); var prices = new Array(24.99, 27.99, 27.99, 32.99, 35.99); populateSelectMenu(descriptions, prices); var descriptionMenu = document.getElementById("descriptionMenu"); addButton = document.getElementById("addItem"); addButton.onclick = function () { firstAvailable = detectAvailable(); firstAvailable = document.getElementsByName("descriptionInput" + firstAvailable); console.log(firstAvailable); firstAvailable.value = descriptionMenu.options[descriptionMenu.selectedIndex].text; } } function populateSelectMenu(descriptions, prices) { var descriptionMenu = document.getElementById("descriptionMenu"); for (var i = 0; i < descriptions.length; i++) { option = document.createElement("option"); option.value = i; option.innerHTML = descriptions[i] + ": $" + prices[i]; descriptionMenu.appendChild(option); } } function detectAvailable() { var descriptionInputs = document.querySelectorAll("input[name^='descriptionInput']"); inputs: for (var i = 0; i < descriptionInputs.length; i++) { if (descriptionInputs[i].value != '') { continue inputs; } else { var firstAvailable = i; break inputs; } } return firstAvailable; }
<form name="calculator" id="calculator"> <div id="userInput"> <select name="descriptionSelect" id="descriptionMenu"></select> <label>Quantity</label> <input type="number" name="quantity" min="1" max="10" size="2" value="1"> <input type="button" value="Add to List" id="addItem"> </div> <table align="center" cellspacing="1" cellpadding="5" width="40%" border="1"> <thead> <tr> <th>Item Description</th> <th>Unit Price</th> <th>Quantity</th> <th>Price</th> </tr> </thead> <tbody> <tr> <td> <input type="text" name="descriptionInput0" value="" size="30"> </td> <td> <input type="text" name="uPriceInput0" value="" size="5"> </td> <td> <input type="text" name="quantityInput0" value="" size="5"> </td> <td> <input type="text" name="tPriceInput0" value="" size="5"> </td> </tr> <tr> <td> <input type="text" name="descriptionInput1" value="" size="30"> </td> <td> <input type="text" name="uPriceInput1" value="" size="5"> </td> <td> <input type="text" name="quantityInput1" value="" size="5"> </td> <td> <input type="text" name="tPriceInput1" value="" size="5"> </td> </tr> <tr> <td> <input type="text" name="descriptionInput2" value="" size="30"> </td> <td> <input type="text" name="uPriceInput2" value="" size="5"> </td> <td> <input type="text" name="quantityInput2" value="" size="5"> </td> <td> <input type="text" name="tPriceInput2" value="" size="5"> </td> </tr> <tr> <td> <input type="text" name="descriptionInput3" value="" size="30"> </td> <td> <input type="text" name="uPriceInput3" value="" size="5"> </td> <td> <input type="text" name="quantityInput3" value="" size="5"> </td> <td> <input type="text" name="tPriceInput3" value="" size="5"> </td> </tr> <tr> <td> <input type="text" name="descriptionInput4" value="" size="30"> </td> <td> <input type="text" name="uPriceInput4" value="" size="5"> </td> <td> <input type="text" name="quantityInput4" value="" size="5"> </td> <td> <input type="text" name="tPriceInput4" value="" size="5"> </td> </tr> <tr> <td colspan="3" align="right">Total Price:</td> <td> <input type="text" name="totalPrice" size="5" disabled> </td> </tr> </tbody> </table> </form>
原文:https://stackoverflow.com/questions/31365078
最满意答案
删除图像上的min-height属性。
.ft-gallery-v2 .ft-class-item .image img { width: 100%; /* min-height: 100%; */ }
Remove the min-height property on the image.
.ft-gallery-v2 .ft-class-item .image img { width: 100%; /* min-height: 100%; */ }
相关问答
更多-
WordPress的网站不在特定的Safari浏览器上显示图像(Wordpress website not displaying images on certain safari browsers)[2022-05-18]
看起来这些资产使用https链接,但网站上没有有效的SSL。 因此,在它工作的设备上,您既可以授权SSL,也可以绕过它,但其他设备则没有。 如果您在Chrome中打开Developer Tools并查看控制台,则可以看到具有问题的资产以及错误消息...“无法加载资源:net :: ERR_INSECURE_RESPONSE” It appears these assets are linked using https but there is not a valid SSL on the site. So ... -
用css进行safari web-kit浏览器页面渲染[关闭](safari web-kit browser page rendering with css [closed])[2022-09-05]
使用Safari Web Inspector,当我从文档head删除此代码时,左侧的空间大小消失: 尝试删除AddThis小部件和代码,看看它是否修复了您的问题。 Using Safari Web Inspector, when I delete this code from the head of th ... -
右键单击csv文件的链接,然后选择“保存目标为”的行 这将阻止打开文件,并允许您将其保存在任何地方:) right click the link to the csv file and select something along the lines of 'save target as' this will prevent opening the file and allow you to save it wherever you wish :)
-
除了Michaels的建议之外,你可以这样做,我使用background-image而不是img (因为你总是使用给定的高度),以及如何在绝对位置添加文本 html, body { margin: 0; } .featured-grid{ display: flex; height: 100vh; } div { position: relative; background-position: center; background-repeat: no-reapa ...
-
删除图像上的min-height属性。 .ft-gallery-v2 .ft-class-item .image img { width: 100%; /* min-height: 100%; */ } Remove the min-height property on the image. .ft-gallery-v2 .ft-class-item .image img { width: 100%; /* min-height: 100%; */ }
-
**最终的解决方案是:1。添加条件IE专门用于在通过IE浏览器2查看时禁用border-radius。删除最后一点不稳定:)我拍摄了我正在使用的所有图像,创建了一个精灵为他们在photoshop然后使用雅虎Smush-It压缩该文件。 该网站现在加载很棒。 谢谢大家的见解! 更新*我也在我的iphone上为我的移动版本在safari手机中遇到了类似的问题。 从容器元素中删除border-radius属性也修复了我的移动网站的速度。 **The final solution was to: 1. Added ...
-
由于这里有Selenium Safari驱动程序 https://code.google.com/p/selenium/wiki/SafariDriver 我假设使用Selenium库在MacOS(以及其他操作系统)中可以实现上述要求。 我在Firefox和Chrome上测试过这个基本功能,正如您在其他问题中发布的那样。 如果那些确实适用于您的要求,那么Safari也可以实现。 但是,这可能需要更多的工作。 Selenium用于软件测试,但我相信它可以用于您的应用程序。 我认为只要Selenium支持它,这 ...
-
在safari中测试网站,而无需在您的计算机上安装Safari(Testing a website in safari without installing safari on your machine)[2022-04-09]
我用这个: http://browsershots.org/ 这对每个浏览器进行预览都很好 I am using this: http://browsershots.org/ This is good to have a preview with each browser -
如果您已清除浏览器缓存,则问题将是服务器端缓存。 您可以使用.htaccess文件在网站上工作时禁用此功能,您可以在此处找到更多信息: 如何防止Apache httpd(MAMP)中的http文件缓存 I tried opening the website using www.willandconfidence.com/home.html This worked. I guess there is a major issue with my mac and its not getting solved wi ...
-
Safari和umbraco网站(Safari and umbraco website)[2023-08-09]
正如你所说,服务器问题。 主办方不提供完全信任。 Umbraco需要完全信任。 您看到的网站现在是旧版本,没有Umbraco。 我将转移到另一个托管提供商。 问候 Exactly as you say, a server issue. The hoster does not provide full trust. Umbraco needs full trust. The website you are seeing, is the old version now, without Umbraco. I w ...