Issue
I want to display an alert message when no option is selected from a dropdown list that is generated from PHP.
I have a script that displays an alert message when the text box is left blank as it was static and easy to do although i am having trouble doing the same with the drop down list, basically the dropdown list is a selection of cars that the seller can offer to an interested buyer and changes dynamically.
Any Help would be much appreciated
HTML/PHP
<form method="POST" id="carForm" action="<?= $this->url(array('cid' => $this->cid), 'approach-to-car') ?>">
<?= CSRF::tokenField() ?>
<div class="row-fluid">
<div class="span2">
Job:
</div>
<div class="span10">
<select name="car" class="input-block-level">
<?php foreach ($this->cars as $car): ?>
<?php $car = Car::getOne($car['id']); ?>
<?/*@var $car Car */?>
<option value="<?= $car->id ?>"><?= $car->get_title() ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="row-fluid">
<div class="span2">
Message:
</div>
<div class="span10">
<textarea name="message" rows="5" id="messageInput" class="input-block-level"></textarea>
</div>
</div>
<div class="row-fluid">
<div class="offset2">
<input type="submit" class="btn btn-primary" id="submitButton" value="Approach" />
</div>
</div>
jQuery
<script type="text/javascript">
$("#carForm").bind('submit', function(e) {
var messageValue = $("#messageInput").val();
if (messageValue === "") {
$("#alertBox").html("Include a message with your offer");
$("#alertBox").removeClass('hide');
e.preventDefault();
return false;
}
return true;
});
This is the jQuery script that currently isn't working for me
<script type="text/javascript">
$("#carForm").bind('submit', function(e) {
var carValue = $("#input-block-level").val();
if (carValue === "") {
$("#alertBox").html("Please add your car before offering buyers");
$("#alertBox").removeClass('hide');
e.preventDefault();
return false;
}
return true;
});
Solution
Check this: http://jsfiddle.net/5ywey/
var carValue = $(".input-block-level").val();
you try to select by id instead of class.
Answered By - mrcrgl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.