简体   繁体   English

如何在输入值中转义单引号?

[英]How to escape single quote in input value?

I have an input whose value I want to get when it's checked.我有一个input ,当它被检查时我想得到它的值。 It has a single quote in the value.它在值中有一个单引号。

When I run my JS I get this error in the console:当我运行我的 JS 时,我在控制台中收到此错误:

Uncaught Error: Syntax error, unrecognized expression: .Oiseaux d'eau"未捕获的错误:语法错误,无法识别的表达式:.Oiseaux d'eau"

 $('.group_oiseau').on('change', function() { /*var group_oiseau = $('input[type=checkbox][name=group_oiseau]:checked').attr('value');*/ $("." + $(this).find('input').val()).each(function() { if ($(this).hasClass('hidden')) { $(this).removeClass('hidden'); console.log($(this).hasClass('hidden')); } else { $(this).addClass('hidden'); console.log($(this).hasClass('hidden')); } }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label class="checkbox group_oiseau"> <input type="checkbox" class="grey checkbox" value="Oiseaux d'eau" name="group_oiseau[]" required> Oiseaux d'eau <div class="checkbox-cust"></div> </label>

 let value = "Oiseaux d'eau"; String(value).replace(/'/g,'&quot;');

when you convert the single quote to html entity the browser will show you a single quote当您将单引号转换为 html 实体时,浏览器将显示单引号

You could just use JQuery to look for all inputs with type checkbox and then run the each() function.您可以只使用 JQuery 查找所有类型为checkboxinputs ,然后运行each() function。

 $('.group_oiseau').on('change', function() { /*var group_oiseau = $('input[type=checkbox][name=group_oiseau]:checked').attr('value');*/ $.each($('input[type=checkbox]'),function(){ if ($(this).hasClass('hidden')) { $(this).removeClass('hidden'); console.log($(this).hasClass('hidden')); } else { $(this).addClass('hidden'); console.log($(this).hasClass('hidden')); } }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label class="checkbox group_oiseau"> <input type="checkbox" class="grey checkbox" value="Oiseaux d'eau" name="group_oiseau[]" required> Oiseaux d'eau <div class="checkbox-cust"></div> </label>

Fix your name attribute a little, remove subscript [] if not in use:稍微修正一下你的 name 属性,如果不使用,请删除下标[]

<input type="checkbox"  class="grey checkbox" value="Oiseaux d'eau" name="group_oiseau" required>

Then this JQuery code will work fine:然后这个 JQuery 代码可以正常工作:

// Get value of radio which is checked
var value = $('input[name=group_oiseau]:checked').val();

// Print that value in the console
console.log(value);

Based on the comments so far it seems that what you are trying to achieve is not really the escaping of an apostrophe but rather the showing and hiding of elements based on checkbox selection.根据到目前为止的评论,您似乎想要实现的并不是真正的撇号 escaping,而是基于复选框选择的元素的显示和隐藏。 If these dependent elements are children or siblings then you can do this with just CSS.如果这些依赖元素是子元素或兄弟元素,那么您只需使用 CSS 即可。 No JS required:无需 JS:

 label {display:inline-block; border:1px dotted grey; vertical-align:top;}.checkbox-cust {display:none;} input[type='checkbox']:checked ~.checkbox-cust {display:block;}
 <label class="checkbox group_oiseau"> <input type="checkbox" class="grey checkbox" value="Oiseaux d'eau" name="group_oiseau[]" required> Oiseaux d'eau <div class="checkbox-cust">custom stuff 1</div> </label> <label class="checkbox group_2"> <input type="checkbox" class="grey checkbox" value="test" name="test" required> test <div class="checkbox-cust">custom stuff 2</div> </label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM