【问题标题】:Element changed by javascript is not submitting由 javascript 更改的元素未提交
【发布时间】:2020-08-19 01:38:41
【问题描述】:

我创建了一个带有文本输入和按钮的简单表单。

<form method="POST">
    <input type="text" name="Itm" id="Itm">
    <input type="submit" value="submit">
</form>

然后我用 JavaScript 的下拉列表替换文本输入。

<script>
    document.getElementById('Itm').outerHTML="<select id='IItm'>";
     x = document.getElementById("IItm");
     var option = document.createElement("option");
     option.text = "Kiwi";
     x.add(option);
</script>

但是,现在通过单击提交按钮,选择的值不会被提交。

注意:我使用以下代码来获取和检查值。

<?php 
    if ($_SERVER['REQUEST_METHOD']=='POST') {
        print_r($_POST);
    }
 ?>

【问题讨论】:

    标签: javascript html post drop-down-menu outerhtml


    【解决方案1】:

    您必须为选择设置value。该值是在 post 中传递的。

    <script>
        document.getElementById('Itm').outerHTML="<select id='IItm'>";
         x = document.getElementById("IItm");
         var option = document.createElement("option");
         option.text = "Kiwi";
         x.value = "Kiwi"
         x.add(option);
    </script>
    

    【讨论】:

    • 感谢您的回答。我尝试了上述建议,但没有奏效。后来我发现为元素指定名称会产生预期的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多