使用JavaScript将checkbox值加载到字符串中 [英] Using JavaScript to load checkbox Values into a string
问题描述
我希望访问者能够做的是:勾选任意数量的要查询的产品的复选框,并在页面底部显示一个链接,当他们勾选第一个。他们点击链接并自动填充表单。
我有一个查询的后半部分,但我需要获取逗号分隔的复选框值,并将它们用于我的链接 - 即如果我的复选框代码是:
< input type =checkboxid =selectedname =selectedvalue =Blueclass =products>< br>< input type =checkboxid =selectedname = selectedvalue =Greenclass =products>< br>< input type =checkboxid =selectedname =selectedvalue =Purpleclass =products> code>
我需要JS生成的字符串: http://example.com/?subject=Products&checked=Blue,Green,Purple
我试过调整此代码(在这里找到:在jQuery中获取所有选中的复选框VALUES的最佳方法),但未成功:
div class =snippetdata-lang =jsdata-hide =falsedata-console =truedata-babel =false>
如何将我的复选框值加载到查询字符串中我需要?
解决方案它看起来像下面的字符串。
$(button)。on(click,function(){var arr = [] ).each(function(){if($(this).is(:checked)){arr.push($(this).val())}})var vals = arr.join )var str =http://example.com/?subject=Products&+ vals console.log(str)})
< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js> ;< / script>< input type =checkboxid =selectedname =selectedvalue =Blueclass =products> Blue< br>< input type =checkboxid =selectedname =selectedvalue =Greenclass =products>绿色< br>< input type =checkboxid =selectedname =selectedvalue =Purpleclass =products>紫色< br>< button>按钮< / button>
What I want visitors to be able to do is: tick any number of checkboxes for products they'd like to enquire about, and have a link appear at the bottom of the page after they've ticked the first. They click the link and it autofills a form.
I've got the latter part sussed using a query, but I need to get comma separated checkbox values and feed them into a string for use in my link - i.e. if my checkbox code is:
<input type="checkbox" id="selected" name="selected" value="Blue" class="products"><br> <input type="checkbox" id="selected" name="selected" value="Green" class="products"><br> <input type="checkbox" id="selected" name="selected" value="Purple" class="products">
I need the string generated by JS to be: http://example.com/?subject=Products&checked=Blue,Green,Purple
I've tried adapting this code (found here: Best way to get all selected checkboxes VALUES in jQuery), without success:
var checkedValues = $('.products:checkbox:checked').map(function() { return this.value; }).get();
How can I load my checkbox values into the query string I need?
解决方案It looks like the below produces the string.
$("button").on("click", function(){ var arr = [] $(":checkbox").each(function(){ if($(this).is(":checked")){ arr.push($(this).val()) } }) var vals = arr.join(",") var str = "http://example.com/?subject=Products&" + vals console.log(str) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <input type="checkbox" id="selected" name="selected" value="Blue" class="products"> Blue<br> <input type="checkbox" id="selected" name="selected" value="Green" class="products"> Green<br> <input type="checkbox" id="selected" name="selected" value="Purple" class="products"> Purple <br> <button>button</button>
这篇关于使用JavaScript将checkbox值加载到字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!