jQuery Passing text input value to plugin option
I can't find quite the answer I'm looking for anywhere. I have a plugin that generates barcodes. All you do is tell it what element to render the barcode, and it spits it out. For example:
$("#barcode-target").barcode("Output Goes Here", "code39",{barWidth:2, barHeight:30});
will put a barcode that says "Output Goes Here" into the div #barcode-target.
However, I need it to generate a barcode with the value of a certain text input. I tried doing something along the lines of
$(function(){
$("#barcode").keyup(function () {
var value = $(this).val();
$("#barcode-target").text(value);
}).key开发者_JS百科up();
});
Which, of course, only puts plain characters in the div. I'm stuck!
Edit I've also tried
$(function(){
var barcodeVal = $("#barcode").val();
$("#barcode-target").barcode("barcodeVal", "code39",{barWidth:2, barHeight:30});
});
Still nothing...
You need to create a barcode the way you said:
$(function(){
$("#barcode").keyup(function () {
$("#barcode-target").barcode($(this).val(), "code39", {
barWidth:2,
barHeight:30
});
}).keyup();
});
With regard to your edit, if you're wanting to pass the value of the barcodeVal
variable, you shouldn't put the name in quotes.
$(function(){
var barcodeVal = $("#barcode").val();
// no quotation marks----------v
$("#barcode-target").barcode( barcodeVal, "code39",{barWidth:2, barHeight:30});
});
Otherwise you were sending a String literal.
Or you could forego the variable, and place the .val()
call directly in the call to barcode()
.
$(function(){
// Get the .val() while calling barcode
$("#barcode-target").barcode( $("#barcode").val(), "code39",{barWidth:2, barHeight:30});
});
精彩评论