Adding Html button and Onclick using Jquery?
Here is what I am trying to do.
function showDescription(td)
{
var desc = $(td).children("input:first").val();
var Button = $('<input type="button" value="Accept" onClick=validateUserInput(开发者_JAVA技巧' + desc + '); style="float:right;"/>');
var dlg = $("#div");
dlg.append(Button);
}
desc is in format of 49.595,-126.592 49.620,-126.602 49.642,-126.606
When I inspect the view source I see it in format.
<input type="button" style="float: right;" 49.620,-126.602);="" 49.642,-126.606="" onclick="validateUserInput(49.595,-126.592" value="something">
How do I indent desc so that its read as one single string.
Thanks.
To have it passed to the function as a single string surround your onclick method in single quotes and the value in double quotes (single always precedes double):
var Button = $('<input type="button"
value="Accept"
onClick=\'validateUserInput("' + desc + '")\';
style="float:right;"/>');
And if you want to parse them later:
function validateUserInput(string desc)
{
var descVals = desc.split(',');
// descVals[0] = "49.595"
// descVals[1] = "-126.592 49.620"
// etc.
}
You need to put quotes around the onClick attribute, otherwise it is treating each set of numbers as an attribute, not a value. Your button code should look like this:
var Button = $('<input type="button" value="Accept" onClick="validateUserInput(' + desc + ');" style="float:right;"/>');
精彩评论