从用户获取两个字符串作为输入,而不是按钮值

时间:2016-09-04 03:47:39

标签: javascript html forms function io

我有这段代码:

<!DOCTYPE html>
<html>
<body>

<form id="frm1" action="form_action.asp">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br><br>
  <input type="submit" value="Submit">
</form>

<p>Click "Try it" to display the value of each element in the form.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("frm1");
    var text = "";
    var i;
    for (i = 0; i < x.length ;i++) {
        if(x.elements[i].value)
            text += x.elements[i].value + "<br>";
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>

将会对待&#34;提交&#34;来自Submit按钮的字符串作为输入字符串。显然,假设用户没有输入&#34;提交&#34;,我可以硬编码 - 检查值是否为&#34;提交&#34;并跳过它。

还有其他方法吗?

3 个答案:

答案 0 :(得分:3)

  

我可以硬编码检查该值是否为“提交”并跳过它。

检查不起作用,因为用户可能实际输入“提交”一词。

您可以测试元素的类型:

if (x.elements[i].type === "text") { ... }

或者您可以直接使用the querySelectorAll() method选择文本元素:

function myFunction() {
    var x = document.querySelectorAll("#frm1 input[type='text']");
    var text = "";
    var i;
    for (i = 0; i < x.length ;i++) {
        text += x[i].value + "<br>";
    }
    document.getElementById("demo").innerHTML = text;
}
<form id="frm1" action="form_action.asp">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br><br>
  <input type="submit" value="Submit">
</form>

<p>Click "Try it" to display the value of each element in the form.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

答案 1 :(得分:2)

仅显示名称为

的元素
for (i = 0; i < x.length ;i++) {
    if(x.elements[i].value && x.elements[i].name)
        text += x.elements[i].value + "<br>";
}

可能比仅选择text输入更有用,因为如果您有其他类型,则不需要重新编码 - 加上,强调这些是在表单提交期间发送的项目。

因为我很无聊:

function myFunction() {
    document.getElementById("demo").innerHTML = 
        Array.prototype.slice.call(document.querySelectorAll("#frm1 input[name]"))
             .reduce( ( prev, curr ) => 
                 { return prev + curr.value + "<br />"; }, "");
}

document.addEventListener( "DOMContentLoaded",
    function() {
        document.getElementById("button").addEventListener("click", myFunction, false);
    }, false );
<body>
<form id="frm1" action="form_action.asp">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br><br>
  <input type="submit" value="Submit">
</form>
<p>Click "Try it" to display the value of each element in the form.</p>
<button id="button">Try it</button>
<p id="demo"></p>
</body>

答案 2 :(得分:2)

您可以检查元素的类型(x.elements[i].type === 'text'):

<!DOCTYPE html>
<html>
<body>

<form id="frm1" action="form_action.asp">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br><br>
  <input type="submit" value="Submit">
</form>

<p>Click "Try it" to display the value of each element in the form.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("frm1");
    var text = "";
    var i;
    for (i = 0; i < x.length ;i++) {
        if(x.elements[i].value && x.elements[i].type === 'text')
            text += x.elements[i].value + "<br>";
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>