gpt4 book ai didi

javascript - 即使没有输入也不允许脚本创建 .txt 文件

转载 作者:行者123 更新时间:2023-12-03 00:58:01 24 4
gpt4 key购买 nike

所以我一直在尝试创建一个 if 语句,即使用户没有在函数“submitOrder”中输入任何内容,该语句也会阻止“submitOrder 按钮”创建 .txt 文件

每次我单击按钮时,即使有文本显示“请输入名称”,也如提到的那样,即使用户没有输入,它也会创建一个 .txt 文件。

编辑:只是为了澄清一下,文本文件被创建并保存到我存储 hta 文件的同一文件夹中。

function submitOrder(file) {

var fso = new ActiveXObject("scripting.filesystemobject")
var newfile = fso.createTextFile(file, true);
var string = "Thank You For Your Order \r\n";
var orderlist = document.getElementById("orderlist");
var regx = /^[A-Za-z]+$/;
var orderfilename = document.getElementById('orderfilename').value;

for (i = 0; i < orderlist.length; i++) {
if (orderlist.options[i].text != "list of ordered items") {
string = string + orderlist.options[i].text + '\r\n';
}
}

if (orderfilename == "") {
alert("please enter a name for your order")
return false;
}

if (!regx.test("orderfilename")) {
alert("must contain text only")
return false;
} else {
alert(string)
return true;
}

newfile.WriteLine(string);
newfile.Close();
}
<body onload="screen();">

<h1>Gigantuano Pizza Company</h1>

<p>Place your order now!</p>

<select id="pizza">
<option value="0.00">Select a pizza</option>
<option value="10.00">Hawaiian - Large Stuffed Crust - $10.00</option>
<option value="8.00">Hawaiian - XL Standard Base - $8.00</option>
<option value="13.50">Hawaiian - XL Stuffed Crust - $13.50</option>
<option value="10.00">Beef and Onion - Large Stuffed Crust - $10.00</option>
<option value="8.00">Beef and Onion - XL Standard Base - $8.00</option>
<option value="13.50">Beef and Onion - XL Stuffed Crust - $13.50</option>
<option value="11.00">Peperoni Lovers - Large Stuffed Crust - $11.00
</option>
<option value="13.50">Chicken Supreme - Large Stuffed Crust - $13.50
</option>
</select>

<input type="button" onclick="addOrderItem()" value="Add Order Item"><br>
<br> Ordered Items: click to check order
<select id="orderlist">
<option value="0.00">list of ordered items</option>
</select>
<input type="button" onclick="removeOrderItem();" value="Remove Order
Item">

<br> Order Total: <input type="text" id="ordertotal" value=0.0></input><br>
<br>
<br> Type your order submission file name (one word):<input type="text" id="orderfilename" value=""></input>
Click to submit your order:
<input type="button" onclick="submitOrder(document.getElementById('orderfilename').value +
'.txt');" value="Submit Order">
<input type="button" value="Open Order Text File" onclick="OpenFile(
document.getElementById('orderfilename').value + '.txt' );" />
<br>

</body>

最佳答案

脚本中存在几个错误,做了一些评论 - 对 ActiveX 的检查不太理想(不是问题的一部分)。

从标记中取出事件处理程序;

请注意,我比问题多做了“一点”,但最重要的是,只有当您有有效的订单时,才应该执行文本文件。

window.onload = screen();
// borrowed from https://stackoverflow.com/a/34729943/125981
function addEventListener(obj, evt, func) {
if ('addEventListener' in window) {
obj.addEventListener(evt, func, false);
} else if ('attachEvent' in window) { //IE
obj.attachEvent('on' + evt, func);
}
}

function screen() {
var bugs = "onscreen";
}
var sobuttonElement = document.getElementById('submitorder');
sobuttonElement.addEventListener('click', submitOrder);
var ofbuttonElement = document.getElementById('openfile');
ofbuttonElement.addEventListener('click', OpenFile);
var aoibuttonElement = document.getElementById('addorderitem');
aoibuttonElement.addEventListener('click', addOrderItem);
var roibuttonElement = document.getElementById('removeorderitem');
roibuttonElement.addEventListener('click', removeOrderItem);
var newLine = '\r\n';

function addOrderItem(event) {
event.target.style.border = "solid 2px orange";
//alert('Element clicked through function!');
var select = document.getElementById('pizza');
var pizzas = select.selectedOptions;
//console.log(pizzas);
var orderList = document.getElementById('orderlist');
for (var i = 0; i < pizzas.length; i++) {
// console.log(pizzas[i]);
var opt = document.createElement('option');
opt.value = pizzas[i].value;
opt.innerHTML = pizzas[i].innerHTML;
orderList.appendChild(opt);
var orderTotal = document.getElementById('ordertotal');
orderTotal.value = (orderTotal.value * 1) + (pizzas[i].value * 1);
}
}

function OpenFile(event) {
var orderfilenameValue = document.getElementById('orderfilename').value;
var file = document.getElementById('orderfilename').value + '.txt'
}

function removeOrderItem() {
var orderList = document.getElementById('orderlist');
if (orderList.selectedIndex > 0) {
orderList.options[orderList.selectedIndex].remove(orderList.selectedIndex);
var orderTotal = document.getElementById('ordertotal');
orderTotal.value = (orderTotal.value * 1) - orderList.options[orderList.selectedIndex].value;
}

}

function getOrderText(message) {
console.log(message);
var orderText = "";
var orderList = document.getElementById('orderlist');
if (orderList.options.length > 1) {
for (var i = 1; i < orderList.options.length; i++) {
//console.log("food:", i, orderList.options[i]);
orderText = orderText + newLine + orderList.options[i].text;
}
orderText = orderText + "\n\r" + message;
}
return orderText;
}

function submitOrder() {
var orderfilenameValue = document.getElementById('orderfilename').value;
var file = orderfilenameValue + '.txt';
alert(orderfilenameValue);
var thankYouMessage = "Thank You For Your Order \r\n";
var orderlist = document.getElementById("orderlist");
var regx = /^[A-Za-z]+$/;
/* moved to a function
for (var i = 0; i < orderlist.length; i++) {
if (orderlist.options[i].text != "list of ordered items") {
thankYouMessage = thankYouMessage + orderlist.options[i].text + '\r\n';
}
}
*/
var isValidText = (orderfilenameValue.length > 0);
if (!isValidText) {
alert("please enter a name for your order");
return false;
}
isValidText = isValidText && regx.test(orderfilenameValue);
if (!isValidText) {
alert("must contain text only");
return false;
} else {
// only if we have a valid name
var newfile = undefined;
if ("ActiveXObject" in window) {
let fso = new ActiveXObject("scripting.filesystemobject");
newfile = fso.createTextFile(file, true);
} else {
alert("Must use Internet Explorer");
}
var orderTotal = document.getElementById('ordertotal');

var message = "Order Total: " + orderTotal.value + newLine + thankYouMessage;
var orderText = getOrderText(message);
console.log(orderText);
// in original code these were never executed
if (newfile) {
newfile.WriteLine(orderText);
newfile.Close();
}
}
}
.row {
margin-top: 1em;
margin-bottom: 1em;
}
<body>
<h1>Gigantuano Pizza Company</h1>
<p>Place your order now!</p>
<p>
<select id="pizza">
<option value="0.00">Select a pizza</option>
<option value="10.00">Hawaiian - Large Stuffed Crust - $10.00</option>
<option value="8.00">Hawaiian - XL Standard Base - $8.00</option>
<option value="13.50">Hawaiian - XL Stuffed Crust - $13.50</option>
<option value="10.00">Beef and Onion - Large Stuffed Crust - $10.00</option>
<option value="8.00">Beef and Onion - XL Standard Base - $8.00</option>
<option value="13.50">Beef and Onion - XL Stuffed Crust - $13.50</option>
<option value="11.00">Peperoni Lovers - Large Stuffed Crust - $11.00
</option>
<option value="13.50">Chicken Supreme - Large Stuffed Crust - $13.50
</option>
</select>
<button id="addorderitem" type="button">Add Order Item</button></p>
<br><label for="orderlist">Ordered Items: click to check order</label>
<select id="orderlist">
<option value="0.00" selected>list of ordered items</option>
</select>
<button type="button" id="removeorderitem" onclick="removeOrderItem();">Remove Order
Item</button>

<div class="row"><label for="ordertotal">Order Total:</label> <input type="text" id="ordertotal" value=0.0></input>
</div>

<div class="row"><label for="orderfilename">Type your order submission file name (one word):</label><input type="text" id="orderfilename" value="" />
</div>
<div class="row">
<label for="submitorder"> Click to submit your order:</label>
<button id="submitorder" type="button">Submit Order</button>
<button id="openfile" type="button">Open Order Text File</button>
</div>
</body>

关于javascript - 即使没有输入也不允许脚本创建 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52739598/

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