【问题标题】:Javascript - Retrieve input value from a selected checkbox and do something with itJavascript - 从选定的复选框中检索输入值并对其进行处理
【发布时间】:2017-06-09 18:26:32
【问题描述】:

我是 javascript 新手,正在寻求帮助。我搜索了不同的论坛,但我的技能并不能很好地帮助我构建一个工作脚本。

我有一个带有 4 个复选框和一个提交按钮的简单表单。每个复选框都有一个唯一的值。该值是 URL 的一部分,当单击提交按钮时,所选复选框的值用于构建链接。它还应该在新窗口中打开链接。

这是我的 HTML

<form name="Form">  
<input type="checkbox" name="box" value="001"><label>box1</label><BR>  
<input type="checkbox" name="box" value="002"><label>box2</label><BR>  
<input type="checkbox" name="box" value="003"><label>box3</label><BR>
<input type="checkbox" name="box" value="004"><label>box4</label><BR>
<br>
<input type="button" value="submit" name="butt" onclick="submit()">
</form>

这是我需要 Javascript 来做的事情:

如果勾选了复选框 获取输入值 使用输入值打开链接 基本链接例如“http://example.com/” 如果勾选了多个复选框并且我单击提交,我希望它打开链接 最后的操作是,例如,如果我选中 box1 并单击提交以在新窗口中打开链接,即http://example.com/001

或者,如果我选中多个框并单击提交,它将单独打开所有链接。

这是我从不同论坛尝试的 Javascript 函数 - 它缺少点点滴滴,可能需要重写。

<script type="text/javascript">
function submit(){

var boxes = document.getElementsByName('box');
var url = "http://www.example.com/";

if ( box.checked === true ) {

}

var box;
for (var i = 0, length = boxes.length; i < length; i++) {
  if (boxes[i].checked) {
    // do whatever you want with the checked radio
    box = labels[i].innerHTML;
    window.alert(url + input-value-goes-here );
  }
}
}
</script>

编辑:我完全忘了提到我不想在 javascript 中输入每个值。如果您可以想象 100 多个复选框.. 必须不断更新脚本将是一场噩梦。

谢谢!

感谢您的帮助。

穆斯塔法

【问题讨论】:

  • 至少显示您编写的用于获取输入值并打开一个新标签的脚本,这将有助于您学习
  • 嗨,Iti,我刚刚更新了帖子以包含我一直在使用的不完整的 javascript 函数。谢谢!
  • 使用javascript框架!例如jquery(或其他)。更容易获取元素的值
  • 根据检查的输入,您应该链接看起来像 ex.com?box=001&box=002.....&box=n 吗?
  • 最后一个链接是要下载的 CSV 文件,它使用链接 + ?csv=1 我完全忘了提到我不想在 javascript 中输入每个值。如果您可以想象 100 多个复选框.. 必须不断更新脚本将是一场噩梦。

标签: javascript html checkbox


【解决方案1】:

试试这个...

<html>
<head>

<script type="text/javascript">

function OpenAll()
{
alert("i am in");
var data = document.forms[0].box;
var i;
for (i=0;i<data.length;i++)
  {
  if (data[i].checked)
    {
    window.open(""+data[i].value);
    }
  }
}

</script>
</head>
<body>
 <form method="POST">
<input type="checkbox" name="box" value="http://www.google.com">Google<br/>  
<input type="checkbox" name="box" value="http://www.facebook.com">FaceBook<br/>  
<input type="submit" value="OPEN" onclick="OpenAll()"/>
</form>
</body>
</html>

【讨论】:

  • 它在新页面上效果很好,但如果我将脚本添加到具有其他表单和复选框的现有页面,它就不起作用。我尝试了故障排除并将其隔离为与其他复选框的冲突。此 javascript 是否适用于特定形式或任何可能失败的想法?警报弹出,但只是没有打开链接。
  • 如果有帮助.. Firefox 调试显示“数据未定义”
  • 我明白了。因此,如果我有 3 种表格,而有问题的是我的第 3 种表格,那么 document.forms[2] 0,1,2,3.. 就可以了!再次感谢您!
【解决方案2】:

我不确定是否可以在一个方法中打开多个选项卡/窗口,因为 window.open 将停止当前 JavaScript 方法的运行。 可以打开多个窗口,但是有一个问题。您必须启用弹出窗口,大多数人默认禁用。然后是浏览器问题:作为开发人员,您无法决定浏览器是打开一个新选项卡还是一个新窗口。您可以做什么来显示多个选项卡/窗口,是使用我在答案底部复制的一段 JavaScript。但请注意,虽然它可以工作,但它只打开一个选项卡,其余的是新的“弹出”窗口(至少在 Chrome 中)。

如果您需要打开一页,此代码确实有效,但只打开一页。

使用 jQuery 可能会让您的生活更轻松:

HTML

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">        </script>
        <script type="text/javascript" src="boxes.js"></script>
    </head>
    <body>
        <input type="checkbox" name="box1" value="box1" id="box1"><label>box1</label><br/>
        <input type="checkbox" name="box2" value="box2" id="box2"><label>box2</label><br/>
        <input type="checkbox" name="box3" value="box3" id="box3"><label>box3</label><br/>
        <input type="checkbox" name="box4" value="box4" id="box4"><label>box4</label><br/>

        <input type="button" value="Submit" name="submitButton" onclick="submit()">
    </body>
</html>

JavaScript (jQuery)

function submit() {
    var box1 = $('#box1');
    var box2 = $('#box2');
    var box3 = $('#box3');
    var box4 = $('#box4');

    if (box1.prop('checked')) {
        goToUrl('http://box1url.com');
        //Redirect by box1
    }
    if (box2.prop('checked')) {
        goToUrl('http://box2url.com');
        //Redirect by box2
    }
    if (box3.prop('checked')) {
        goToUrl('http://box3url.com');
        //Redirect by box3
    }
    if (box4.prop('checked')) {
        goToUrl('http://box4url.com');
        //Redirect by box4
    }
}

function goToUrl(url) {
    var win = window.open(url, '_blank');
    win.focus();
}

编辑:没有 JQUERY

如果你决定不使用 jQuery,你可以将它用于 JavaScript:

function submit() {
    var box1 = document.getElementById('box1');
    var box2 = document.getElementById('box2');
    var box3 = document.getElementById('box3');
    var box4 = document.getElementById('box4');

    if (box1.checked) {
        goToUrl('http://box1url.com');
        //Redirect by box1
    }
    if (box2.checked) {
        goToUrl('http://box2url.com');
        //Redirect by box2
    }
    if (box3.checked) {
        goToUrl('http://box3url.com');
        //Redirect by box3
    }
    if (box4.checked) {
        goToUrl('http://box4url.com');
        //Redirect by box4
    }
}

function goToUrl(url) {
    var win = window.open(url, '_blank');
    win.focus();
}

EDIT2:添加了“多网址”

var urls = []; //Initialize empty array
if (box1.checked) {
    urls.push('http://box1url.com'); // If box is checked, push the url
}

urls.forEach(function(url) {
    goToUrl(url); //Go to all URLs in a for-each loop
});

EDIT3:按值添加了更多“动态”网址

如果将 URL 设置为复选框的值,您可以随后在 JavaScript 中检索它:

HTML:

<input type="checkbox" name="box1" value="http://box1url.com" id="box1"><label>box1</label><br/>

JavaScript(无 jQuery):

if (box1.checked) {
        urls.push(box1.value);
        //Redirect by box1
}

JavaScript (jQuery):

if (box1.prop('checked')) {
        urls.push(box1.value);
        //Redirect by box1
}

EDIT4:完整代码

<html>
    <head>
        <meta charset="UTF-8">
        <title></title> 
        <script type="text/javascript" src="no-j-boxes.js"></script>
    </head>
    <body>
        <input type="checkbox" name="box1" value="http://box1url.com" id="box1"><label>box1</label><br/>
        <input type="checkbox" name="box2" value="http://box2url.com" id="box2"><label>box2</label><br/>
        <input type="checkbox" name="box3" value="http://box3url.com" id="box3"><label>box3</label><br/>
        <input type="checkbox" name="box4" value="http://box4url.com" id="box4"><label>box4</label><br/>

        <input type="button" value="Submit" name="submitButton" onclick="submit()">
    </body>
</html>

JavaScript:

function submit() {
    var box1 = document.getElementById('box1');
    var box2 = document.getElementById('box2');
    var box3 = document.getElementById('box3');
    var box4 = document.getElementById('box4');

    var urls = [];

    if (box1.checked) {
        urls.push(box1.value);
    }
    if (box2.checked) {
        urls.push(box2.value);
    }
    if (box3.checked) {
        urls.push(box3.value);
    }
    if (box4.checked) {
        urls.push(box4.value);
    }

    urls.forEach(function(url) {
        goToUrl(url);
    });
}

function goToUrl(url) {
    var win = window.open(url, '_blank');
}

【讨论】:

  • Pim,哇,感谢您提供如此全面的回复和使用 Jquery 的选项。我没有玩过jquery,但我会尝试一下。非常抱歉,我完全忘记提及我宁愿不必在 javascript 中输入每个值。如果您可以想象 100 多个复选框.. 必须不断更新脚本将是一场噩梦。
  • 我添加了一种从复选框的值中检索 URL 的方法。假设您有一个 PHP 脚本循环遍历创建复选框所需的数据,只需让循环将值设置为特定的 URL。然后,您可以使用 box1.value 使用 JavaScript 检索它。希望这对你有帮助:)
  • Pim,对不起,完整的代码应该是什么样子。我想你可能已经写了我要找的东西,但我很难把它们放在一起。我可以麻烦您发布完整的代码“无 jquery 版本”另外,这可以与我拥有的提交按钮一起使用吗?阅读您的代码,似乎只需选中该框即可打开 URL。
  • 另一个编辑,虽然我可能会稍微清理一下帖子:)。如果您希望它更具动态性,请使用一个提示:使用类。给你的复选框一个类,这样你就可以使用 document.getElementsByClassName(className); 循环遍历这些类然后你可以用你的复选框循环遍历返回的数组。
  • 很高兴为您提供帮助:)
猜你喜欢
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多