admin管理员组文章数量:1389931
I have a jQuery script that collects the values of checkboxes in variables:
var 1_val = $("input[type='checkbox'][name='val_1']").prop( "checked" );
var 2_val = $("input[type='checkbox'][name='val_2']").prop( "checked" );
Then I output these in a message to be sent via e-mail:
...<strong>Value 1</strong>: 1_val + '<br /><strong>Value 2</strong>: ' + 2_val + '<br />...
But in the message body I get the string with booleans true/false, and I would want to make some more user-friendly message like Yes/No. How can I change it?
I have a jQuery script that collects the values of checkboxes in variables:
var 1_val = $("input[type='checkbox'][name='val_1']").prop( "checked" );
var 2_val = $("input[type='checkbox'][name='val_2']").prop( "checked" );
Then I output these in a message to be sent via e-mail:
...<strong>Value 1</strong>: 1_val + '<br /><strong>Value 2</strong>: ' + 2_val + '<br />...
But in the message body I get the string with booleans true/false, and I would want to make some more user-friendly message like Yes/No. How can I change it?
Share Improve this question edited Mar 21, 2014 at 18:12 user229044♦ 240k41 gold badges344 silver badges346 bronze badges asked Mar 21, 2014 at 18:11 GasGas 7372 gold badges14 silver badges32 bronze badges 04 Answers
Reset to default 6Assign that value to the variable:
var 1_val = $("input[type='checkbox'][name='val_1']").prop( "checked" ) ? "Yes" : "No";
You can use the ternary operator (which is essentially a short if-statement):
(2_val ? 'Yes' : 'No')
The value of this expression will be the string Yes
if 2_val == true
, else No
.
You could replace 1_val
with (1_val ? 'Yes' : 'No')
when you output it. (and do the same for 2_val
) Alternatively, if you only use these variables for this output, you could do what tymeJV suggests.
Probably far from being the best solution, but one way would be:
var YesNo = {
'true' : 'Yes',
'false' : 'No'
};
And use like this on your HTML fragment:
var html = '<strong>Value 1 :</strong>'
+ YesNo[val_1] + '<strong>Value 2</strong>' + YesNo[val_2];
本文标签: jqueryJavascript convert a truefalse value string textStack Overflow
版权声明:本文标题:jquery - Javascript convert a truefalse value string text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744589438a2614395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论