连接两个字段并使用此值来更改/设置隐藏字段的值

时间:2018-02-13 10:25:35

标签: javascript html

这是我想要实现的目标。我要求国家代码有一个下拉列表,手机号码有另一个输入字段。我想将国家代码和移动输入值作为组合值发送,因此我使用隐藏字段。不使用隐藏字段时,很容易更改第三个标记元素的值,但在提交表单时不会发送值。所以必须使用隐藏的领域。所以我尝试过这样做,但它没有改变隐藏字段的价值。

<html>
<head>
    <script language="javascript">
    var dropdown = '';
    var mobilenum = '';

    function calldropdown()
    {
        dropdown = document.getElementById("country_code_id").value;
        return dropdown;
    }

    function calltxtfield()
    {
        mobilenum = document.getElementById("mobileid").value;
        return mobilenum;
    }

    function codemobile()
    {
        document.getElementById("codemobileid").value = calldropdown() + ' ' + calltxtfield;
        alert(document.getElementById("codemobileid").value);
    }
    </script>
</head>
<body>
    <form>
        <select name="country_code" id="country_code_id" onchange="calldropdown()">
            <option value="">Select</option>
            <option value="+975">Bhutan</option>
            <option value="+977">Nepal</option>
            <option value="+94">Sri Lanka</option>
        </select>
        <input type="text" name="mobile" id="mobileid" onchange="calltxtfield()" />

        <input type="hidden" name="codemobile" id="codemobileid" value="" />
        <input type="submit" name="submit" value="Submit" onclick="return codemobile();" />
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

我现在无法解释,但name="codemobile"似乎默默地暗示function codemobile()。重命名其中一个并且它可以工作。

另请注意,现在您正在连接函数体(... + calltxtfield;),它应该是... + calltxtfield();