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 0
Add a ment  | 

4 Answers 4

Reset to default 6

Assign 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