Javascript 提交表单时将输入文本转换为小写

Javascript 提交表单时将输入文本转换为小写,javascript,php,jquery,html,forms,Javascript,Php,Jquery,Html,Forms,我有一个表单,其中有一个文本字段和一个提交按钮。单击提交按钮,它会从第一个php页面重定向到第二个php页面。 index.php <form action="submit.php" method="get"> <input type="text" name="search" id="search" /> <input type="submit" value="submit" onclick="convert()" /> </form <sc

我有一个表单,其中有一个文本字段和一个提交按钮。单击提交按钮,它会从第一个php页面重定向到第二个php页面。 index.php

<form action="submit.php" method="get">

<input type="text" name="search" id="search" />
<input type="submit" value="submit" onclick="convert()" />
</form 

<script type="text/javascript">
function convert() 
{
    alert("hi");
    var str ;
    str = document.getElementById("search").value;
    document.writeln(str.toLowerCase());
}
</script>


您只能使用javascript和一些额外的东西来实现这一点:

1) 给你的
一个
id

<form action="submit.php" method="get" id="form1">
3) 最后,请注意:

function convert() 
{
    alert("hi");
    var str ;
    str = document.getElementById("search");
    str.value = (str.value.toLowerCase());
    //get the form id and submit it
    var form = document.getElementById("form1");
    form.submit();
}

出现一些错误,您缺少
上的直角括号,并且您试图写入值而不是设置字段值,请尝试以下操作

<form action="submit.php" method="get">

<input type="text" name="search" id="search"  />
<input type="submit" value="submit" onclick="convert();" />
</form> 

<script type="text/javascript">
    function convert() {
        alert("hi");
        var str;
        var srch=document.getElementById("search");
        str = srch.value;
        srch.value=str.toLowerCase();
    }
</script>

函数转换(){
警报(“hi”);
var-str;
var srch=document.getElementById(“搜索”);
str=srch.value;
srch.value=str.toLowerCase();
}
试着这样做:

alert("hi");
document.getElementById("search").value =  document.getElementById("search").value.toLowerCase();
return true;

您正在使用表单元素,因此您可以获取表单元素内部的任何元素按名称访问,这里我们的表单名称是表单1,并且在该表单的输入框内name=“search”并通过此方式访问此值
document.form1.search.value.toLowerCase()

选中此项 JavaScript HTML
document.writeln(str.toLowerCase())
可以是
document.writeln(str)
。感谢您通知我并支持我,我的答案@AmalMurali现在已更新。
alert("hi");
document.getElementById("search").value =  document.getElementById("search").value.toLowerCase();
return true;
function convert() {
    alert("hi");
    var str = document.form1.search.value.toLowerCase();
    document.writeln(str);
    //console.log(str);
}
<form name="form1" method="get">
    <input type="text" name="search" id="search" />
    <input type="submit" value="submit" onclick="convert();" />
</form >