javascript - Issue in showing error to the particular spans -
the info json page similar
data :
{"busno":["the bus no field required."],"compid":["the comp id field required."],"totalseats":["the total seats field required."]}
here script :
$(document).ready(function() { $("#driver").click(function(event) { var busno = $("#busno").val(); var compid = $("#compid").val(); var totalseats = $("#totalseats").val(); var _token = $("#_token").val(); $.post("managebus_register", { _token: _token, busno: busno, compid: compid, totalseats: totalseats }, function(data) { if (data != '') { var obj = json.parse(data); $.each(obj, function(entry) { var targetselector = ''; if (entry == "busno") { targetselector = "#busno"; console.log('error in bus field'); } if (entry == "compid") { targetselector = "#compid"; console.log('error in comp id'); } if (entry == "totalseats") { targetselector = "#totalseats"; console.log('error in total seats'); } if (targetselector) $(targetselector).next("span.error").html(obj[entry]); else { $(targetselector).next("span.error").text(' '); } }); } else { console.log('pass'); } }); }); });
the status goes within if if(data != '')
, checks targetselector = "#busno";
my problem 1 time script run throw error particular spans i.e., busno, compid etc.,
and 1 time filled keeps on showing error messages
that is, given script
if (targetselector) $(targetselector).next("span.error").html(obj[entry]); else { $(targetselector).next("span.error").text(' '); }
here else part $(targetselector).next("span.error").text(' ');
not working,
what error doing , how can prepare this.
update :i got know
$("span.error").empty();
will this
but need highlight input text box error , error should shown in hover of particular text , not in
<input type="text" id="busno" name="busno"/><span class="error"></span> <input type="text" id="compid" name="compid"/><span class="error"></span>
changes in code:
if (targetselector) $(targetselector).next("span.error").html(obj[entry][0]); else { $(targetselector).next("span.error").text(' '); }
if want show error on hover:
$("input").hover(function(){ var id = $(this).attr("id"); $("input").next("span.error").hide(); $("#"+id).next("span.error").show(); })
javascript jquery html css json
No comments:
Post a Comment