表单选择选项导致javascript失败

时间:2018-04-09 18:36:58

标签: javascript

我有以下javascript函数,一旦选择了select选项就会重新加载表单。我正在使用它将数据从数据库中提取到每个额外的选择下拉列表,这取决于他们在第一个,第二个等中选择的选项。

function reloadForm(form)
{
var cat=form.cat.options[form.cat.options.selectedIndex].value;
if (cat.length > 0) { var cat1 = '&cat=' + cat; }                    

var subcat1=form.subcat.options[form.subcat.options.selectedIndex].value;
if (subcat1.length > 0) { var subcat2 = '&subcat=' + subcat1; } 

self.location = 'index.php<?php echo $webvars; ?>' + cat1;
}

但这是我的问题,一旦我添加了subcat变量,脚本就会停止工作。我不明白为什么?

以下是我的html / php代码到目前为止....以防有人需要它。

$product_sku = $db1q->query("SELECT sku FROM Inventory_SKU WHERE id = $id");
if ($product_sku->num_rows > 0) { 
while ($row = $product_sku->fetch_assoc()) { echo '<h2>CONNECTING '. $row['sku'] .' TO PRODUCT</h2>'; }
}

echo '<div class="margin20">';                                
echo '<form name="connectsku" action="index.php'. $webvars .'" method="POST">';
echo '<input type="hidden" name="connect_sku" value="1">';                               




echo '<select name="cat" onchange="reloadForm(this.form)"><option value="">SELECT CATEGORY</option>';
foreach ($db1q->query("SELECT c.id, c.name,s.name as section FROM Product_Categories as c JOIN Product_Sections as s ON c.section = s.id") as $catdd) {
echo '<option value="'. $catdd['id'] .'">'. $catdd['name'] .'</option>';
}
echo '</select>';




if ((isset($cat)) && ($cat != "")) {                                               
echo '<select name="subcat" onchange="reloadForm(this.form)">
<option value="">Select SUBCAT</option>';
foreach ($db1q->query("SELECT id, name FROM Product_Sub_Category ") as $subcatdd) {
echo '<option value="'. $subcatdd['id'] .'">'. $subcatdd['name'] .'</option>';
}
echo "</select>";

}




if ((isset($subcat)) && ($subcat != "")) {
echo "<select name='subcat3' ><option value=''>Select one</option>";
foreach ($dbo->query($quer3) as $noticia) {
echo  "<option value='$noticia[subcat2]'>$noticia[subcat2]</option>";
}
echo "</select>";
}                             

echo '<input type="submit" value="MOVE TO NEXT STEP">';
echo '</form>';
    echo '</div>';

CONSOLE LOG

Uncaught TypeError: Cannot set property 'oninput' of null
    at HTMLDocument.<anonymous> (index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:65)
    at c (jquery-1.10.2.min.js:4)
    at Object.fireWith [as resolveWith] (jquery-1.10.2.min.js:4)
    at Function.ready (jquery-1.10.2.min.js:4)
    at HTMLDocument.q (jquery-1.10.2.min.js:4)
index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:238 Uncaught TypeError: Cannot read property 'options' of undefined
    at reloadForm (index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:238)
    at HTMLSelectElement.onchange (index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:248)

隐藏Subcat Select字段,直到$ cat由url update定义。可能是因为没有什么东西可以从导致错误的页面上拉出来吗?是否有一行代码我可以添加以检查subcat是否具有有效值或因为运行subcat部分而存在?

1 个答案:

答案 0 :(得分:0)

发现这是因为没有生成subcat的select字段,因为我的条件是它只在cat被设置时生成。我最终总是显示它,如果类别没有设置,只使用html“隐藏”它。这样javascript至少可以检测到它在那里。这最终解决了整个问题。