我正在研究一个根据选择下拉菜单生成的警报按钮。JavaScript 已经能够<select>
完全按照我的意愿读取和发送下拉列表的值。但是,当涉及到显示触发警报消息的按钮时,这是我的问题。
问题
-- 我不太确定我是否真的正确删除了孩子(语法问题)
- 我似乎无法显示按钮。相反,它只执行它的onclick
命令。
JAVASCRIPT
function displayInfo(key) {
alert(key);
}
function determineDisplay(Item, ButtonLocation) {
// Get selected value from drop down
var si = Item.selectedIndex;
var sv = Item.options[si].value;
// Create View Button to display info
var VB = document.createElement("input");
// Assign attributes to button
VB.setAttribute("type", "button");
VB.setAttribute("value", "view");
VB.setAttribute("display", "inline-block");
VB.onClick = displayInfo(sv);
// Insert button
var insertHere = document.getElementsById(ButtonLocation);
var CheckChild = insertHere.lastChild;
if(lastChild) {
insertHere.removeChild(CheckChild);
insertHere.appendChild(VB);
}
else
insertHere.appendChild(VB);
}
HTML
<div id="SoupBox">
<select id="Soup" name="Soup" onchange="determineDisplay(this, 'contentView_Two');">
<option>--Select--</option>
<?php foreach($soup_products as $key => $product) :
$name = $product['name']; $cost = number_format($product['cost'], 2);
$item = $name . ' ($' . $cost . ')';
?>
<option value="<?php echo $key; ?>"><?php echo $item; ?></option>
<?php endforeach; ?>
</select>
<input name="s_qty" type="text" placeholder="qty" size="1" maxlength="1" />
<label id="contentView_Two"></label>
</div>
没有 PHP 的 HTML
<div id="SoupBox">
<select id="Soup" name="Soup" onchange="determineDisplay(this, 'contentView_Two');">
<option>--Select--</option>
<option value="CN-CF">Healthy Choice Hearty Chicken ($1.31)</option>
<option value="CN-CT">Pacific Foods Organic Creamy Tomato ($3.75)</option>
<option value="CN-LT">Amy's Organic Lentil Vegetables ($2.62)</option>
<option value="ND-BF">Ramen beef Noddles ($0.50)</option>
<option value="ND-CF">Ramen Chicken Noddles ($0.50)</option>
<option value="ND-LS">Ramen Lime Shrimp Noddles ($0.50)</option>
<option value="ND-SF">Ramen Shrimp Noddles ($0.50)</option>
</select>
<input name="s_qty" type="text" placeholder="qty" size="1" maxlength="1" />
<label id="contentView_Two"></label>
</div>